5

我正在尝试在我的项目(Django 1.6、Python 2.7)中使用Django-Auth-Ldap,但它不起作用。

我的活动目录 shema 是:

在此处输入图像描述

  1. 我已经通过安装 ldap-utils 包测试了 cmd 行上的连接

    sudo apt-get install ldap-utils
    
    ldapsearch -H ldap://domain.com -D "ou=Resources,ou=Company, dc=domain,dc=com" -U "user_name" -w "user_password" -v -d 1
    

连接测试工作正常。

  1. 我正在使用以下代码从 shell 测试 python-ldap 连接:

    import ldap
    
    con = ldap.initialize('ldap://domain.com')
    
    con.simple_bind_s('User_mail', 'User_password')
    
    results = con.search_s('ou=Users,ou=Resources,ou=Company,dc=domain,dc=com', ldap.SCOPE_SUBTREE, "(cn=User_name)")
    

python-ldap 连接工作正常。

  1. 我的问题是如何从我的 django 登录界面对 AD 用户进行身份验证?

设置.py:

import ldap 
from django_auth_ldap.config import LDAPSearch

# The URL of the LDAP server.
AUTH_LDAP_SERVER_URI = "ldap://domain.com"
AUTH_LDAP_BIND_DN = "cn='User_name',ou=Resources,ou=Company,dc=domain,dc=com"   
AUTH_LDAP_BIND_PASSWORD = "User_password"
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=Users,ou=Resources,ou=Company,dc=domain,dc=com",ldap.SCOPE_SUBTREE, "(cn=%(user)s)")    
AUTH_LDAP_GLOBAL_OPTIONS = { ldap.OPT_REFERRALS : False }

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

视图.py:

from django_auth_ldap.backend import LDAPBackend
auth = LDAPBackend()
user = auth.authenticate(username="User_name", password="User_password")

在文件 django-ldap-debug.log 我有这个错误:

Caught LDAPError while authenticating User_name: INVALID_CREDENTIALS({'info': '80090308: LdapErr: DSID-0C0903A9, comment: AcceptSecurityContext error, data 52e, v1db1', 'desc': 'Invalid credentials'},)
4

2 回答 2

9

我找到了答案。

我通过添加 (OU=Users)更改了AUTH_LDAP_BIND_DN

我必须在AUTH_LDAP_USER_SEARCH中使用 samAccountName 而不是 CN

我的新 settings.py :

import ldap, logging
from django_auth_ldap.config import LDAPSearch

logger = logging.getLogger('django_auth_ldap')
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.DEBUG)

AUTH_LDAP_SERVER_URI = "ldap://domain.com"
AUTH_LDAP_BIND_DN = "CN=User_name,OU=Users,OU=Resources,OU=Company,DC=domain,DC=com"
AUTH_LDAP_BIND_PASSWORD = "User_password"
AUTH_LDAP_USER_SEARCH = LDAPSearch("OU=Users,OU=Resources,OU=Company,DC=domain,DC=com",ldap.SCOPE_SUBTREE, "(samAccountName=%(user)s)")

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

我的观点.py

from django_auth_ldap.backend import LDAPBackend

def login(request):  
    if request.method == 'POST':
        form = MyLoginForm(data=request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']            
            auth = LDAPBackend()
            user = auth.authenticate(username=username, password=password)
            if user is not None:
                ....
    else:
        form = MyLoginForm()

    ....

希望这对所有人都有帮助:)

于 2016-10-17T16:44:39.503 回答
0

我有类似的问题,但我已经以其他方式解决了。下AUTH_LDAP_BIND_DN刚刚放了用户和域和密码。在那之后,这就像魅力一样......

当我在调查我的 ldaps 问题时,我发现上述解决方案在某种程度上很有用,也许有人会从我的解决方案中受益

LDAP_IGNORE_CERT_ERRORS = True
AUTH_LDAP_START_TLS = False

AUTH_LDAP_SERVER_URI = "ldaps://domain.com:636" 
AUTH_LDAP_BIND_DN = "serviceaccount@domain.com"
AUTH_LDAP_BIND_PASSWORD = "superPass"
AUTH_LDAP_USER_SEARCH = LDAPSearch(
        "OU=Company,DC=domain,DC=com",ldap.SCOPE_SUBTREE,"(sAMAccountName=%(user)s)"
        )

AUTH_LDAP_USER_ATTR_MAP = {
       "first_name": "givenName",
       "last_name": "sn",
       "email": "mail"
       }
于 2021-09-22T15:09:33.467 回答