在 Active Directory 中使用 django-auth-ldap(身份验证未能将用户名映射到 DN)

NBa*_*nca 1 python django ldap active-directory

我正在尝试使用django-auth-ldap来通过我的 Active Directory 对 Django 中的用户进行身份验证,但没有成功。

我的活动目录有以下树:

  • DC=测试,DC=本地
    • CN=用户
      • CN=管理员
      • CN=测试

我有(在settings.py中):

AUTH_LDAP_SERVER_URI = "ldap://something.test.local"

AUTH_LDAP_BIND_DN = "cn=Administrator,cn=Users,dc=test,dc=local"
AUTH_LDAP_BIND_PASSWORD = "password"
AUTH_LDAP_USER_SEARCH = LDAPSearch("cn=Users,dc=test,dc=local",
ldap.SCOPE_SUBTREE, "(uid=%(user)s)")
Run Code Online (Sandbox Code Playgroud)

我总是得到(在 Django shell 中调试):

>>> from django.contrib.auth import authenticate, login
>>> authenticate(username='test', password='password')
search_s('cn=Users,dc=test,dc=local', 2, '(uid=%(user)s)') returned 0 objects:
Authentication failed for test: failed to map the username to a DN.
Run Code Online (Sandbox Code Playgroud)

NBa*_*nca 7

哪里有:

AUTH_LDAP_USER_SEARCH = LDAPSearch("cn=Users,dc=test,dc=local",
ldap.SCOPE_SUBTREE, "(uid=%(user)s)")
Run Code Online (Sandbox Code Playgroud)

它应该是:

AUTH_LDAP_USER_SEARCH = LDAPSearch("cn=Users,dc=test,dc=local",
ldap.SCOPE_SUBTREE, "(sAMAccountName=%(user)s)")
Run Code Online (Sandbox Code Playgroud)

因为 uid 在 Active Directory 中通常为空,所以搜索不会返回假装的用户。

不,我得到:

search_s('cn=Users,dc=test,dc=local', 2, '(uid=%(user)s)') returned 1 objects: cn=test,cn=users,dc=test,dc=local
Created Django user test
Populating Django user test
<User: test>
Run Code Online (Sandbox Code Playgroud)