django-auth-ldap验证失败

问题描述 投票:3回答:1

我正在尝试在我的项目中使用Django-Auth-Ldap(Django 1.6,Python 2.7),但它无法正常工作。

我的活动目录shema是:

enter image description here

  1. 我已经通过安装ldap-utils包在命令行上测试了连接 qazxsw poi

连接测试工作正常。

  1. 我使用下面的代码来测试shell中的python-ldap连接: 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

python-ldap连接正常。

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

settings.朋友:

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)")

views.朋友:

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',
)

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

from django_auth_ldap.backend import LDAPBackend
auth = LDAPBackend()
user = auth.authenticate(username="User_name", password="User_password")
django authentication active-directory ldap django-auth-ldap
1个回答
4
投票

我找到了答案。

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

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

我的新settings.py:

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

我的views.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',
)

希望这有助于所有:)

© www.soinside.com 2019 - 2024. All rights reserved.