通过Active Directory 2008实现Django LDAP身份验证

5

我正在尝试通过公司的Active Directory服务器对用户进行身份验证。

我无法正确配置它,我知道LDAP是可用的,因为我有一个已经配置并能够在Active Directory服务器上进行身份验证的MediaWiki。

系统:

    Active Directory 2008
    Django (1, 3, 0, 'final', 0)
    django-auth-ldap 1.0.9

这是我的settings.py配置:

    from django_auth_ldap.config import LDAPSearch
    import logging

    # makes sure this works in Active Directory
    ldap.set_option(ldap.OPT_REFERRALS, 0)

    LDAP_SERVER_URI = 'ldap://ad.exemple.com'
    AUTH_LDAP_BIND_DN = 'my_user'
    AUTH_LDAP_BIND_PASSWORD = 'my_pass'
    AUTH_LDAP_USER_SEARCH = LDAPSearch('dc=exemple,dc=com', ldap.SCOPE_SUBTREE, '(SAMAccountName=%(user)s)')

    # Populate the Django user from the LDAP directory.
    AUTH_LDAP_USER_ATTR_MAP = {
            "first_name": "givenName",
            "last_name": "sn",
            "email": "mail"
    }

    # This is the default, but I like to be explicit.
    AUTH_LDAP_ALWAYS_UPDATE_USER = True

    AUTHENTICATION_BACKENDS = (
            'django_auth_ldap.backend.LDAPBackend',
            'django.contrib.auth.backends.ModelBackend',
    )

我也尝试了通过添加新的URI来使用SSL方式

    LDAP_SERVER_URI = 'ldaps://ad.exemple.com:636'

我使用主要用户组来搜索,不需要任何我想要进行身份验证的特定组。

返回的错误消息为:

    WARNING 2011-05-31 16:50:19,429 backend 3968 140632428340992 Caught LDAPError while authenticating my_user: INVALID_CREDENTIALS({'info': '80090308: LdapErr: DSID-0C0903AA, comment: AcceptSecurityContext error, data 525, v1772', 'desc': 'Invalid credentials'},)
    [31/May/2011 16:50:19] "POST /admin/ HTTP/1.1" 200 9648

看到这个错误并寻找LDAP错误DSID-0C0903AA,我发现应该尝试像这样设置用户名

my_user@exemple.com

它没有起作用,返回错误信息:
    ERROR 2011-05-31 16:55:38,947 config 6505 139868662060800 search_s('dc=ubilium,dc=loc', 2, '(SAMAccountName=my_user)') raised OPERATIONS_ERROR({'info': '000004DC: LdapErr: DSID-0C0906DD, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, v1772', 'desc': 'Operations error'},)
    DEBUG 2011-05-31 16:55:38,947 backend 6505 139868662060800 Authentication failed for my_user

有人知道如何连接吗?
1个回答

1

你遇到的第一个错误是LDAP 49,子代码为525,这意味着用户未找到。也就是说,你的绑定DN不正确。

你的第二次尝试,使用userPrincipalName格式化将失败,因为你的配置如下: AUTH_LDAP_USER_SEARCH = LDAPSearch('dc=exemple,dc=com', ldap.SCOPE_SUBTREE, '(SAMAccountName=%(user)s)')

因此,你正在尝试在过滤器中使用传入的用户名:(SAMAccountName=%(user)s)

我想知道最后是否有一个额外的s?也就是说,(SAMAccountName=%(user))更正确吗?

它的作用是说,对于$(user)变量,在AD中查找其sAMAccountName属性与该值匹配的对象,然后使用返回的DN作为绑定DN。但你没有得到正确的DN,因此出现了LDAP 49 - 525错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接