使用 django-auth-ldap 绑定到 Active Directory

Binding to Active Directory using django-auth-ldap

我正在尝试使用 django-auth-ldap 通过 Active Directory 在我的 django 应用程序中创建用户登录身份验证。问题是我无法使用 username(相当于 sAMAccountName LDAP)绑定到 AD。下面是我的settings.py部分:

import ldap
from django_auth_ldap.config import LDAPSearch

AUTHENTICATION_BACKENDS = [
    'django_auth_ldap.backend.LDAPBackend',
]

AUTH_LDAP_START_TLS = False
AUTH_LDAP_ALWAYS_UPDATE_USER = False
AUTH_LDAP_SERVER_URI = 'ldap://ip_address:389'
AUTH_LDAP_BIND_DN = ''
AUTH_LDAP_BIND_PASSWORD = ''
AUTH_LDAP_USER_SEARCH = LDAPSearch('DC=example,DC=com', ldap.SCOPE_SUBTREE, '(sAMAccountName=%(user)s)')
AUTH_LDAP_CONNECTION_OPTIONS = {
    ldap.OPT_REFERRALS: 0,
}

控制台日志:

ERROR search_s('DC=example,DC=com', 2, '(sAMAccountName=user)') raised OPERATIONS_ERROR({'desc': 'Operations error', 'info': '00000000: LdapErr: DSID-0C090627, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, vece'})
DEBUG search_s('DC=example,DC=com', 2, '(sAMAccountName=%(user)s)') returned 0 objects:
DEBUG Authentication failed for user: failed to map the username to a DN.

知道为什么这不起作用吗?

默认情况下不启用匿名读取访问权限。要执行搜索操作,请使用有效帐户填充 AUTH_LDAP_BIND_DN 和 AUTH_LDAP_BIND_PASSWORD。我通常创建专用 "system" 帐户(即不是真人帐户,因为每次用户更改密码时您的身份验证都会失败)。