如何用django-auth-ldap在AD中用cn(通用名)实现认证?

时间:2017-02-09 12:12:38

标签: python authentication active-directory ldap

我在Windows服务器2008 R2上使用Active目录。我有一个运行Django和python 2.7的应用程序。现在我需要使用Active Directory身份验证来访问我的应用程序。

要做到这一点,我正在使用这个包:

sudo apt-get-update
sudo apt-get install python-dev libldap2-dev libsasl2-dev libssl-dev
sudo pip install django-auth-ldap 
sudo pip install python-ldap

我使用全名来成功绑定。

import ldap
from django.conf import settings

username='my full name in AD'
password= 'my password'

l = ldap.initialize(settings.AUTH_LDAP_SERVER_URI)
l.simple_bind_s(username,password)

enter image description here

我的问题是当我尝试使用我的cn属性进行绑定时,我收到了这个错误:

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/ldap/ldapobject.py", line 207, in     
simple_bind_s
return self.result(msgid,all=1,timeout=self.timeout)
File "/usr/lib/python2.7/dist-packages/ldap/ldapobject.py", line 422, in 
result res_type,res_data,res_msgid = self.result2(msgid,all,timeout)
File "/usr/lib/python2.7/dist-packages/ldap/ldapobject.py", line 426, in  
result2 res_type, res_data, res_msgid, srv_ctrls =       
self.result3(msgid,all,timeout)
File "/usr/lib/python2.7/dist-packages/ldap/ldapobject.py", line 432, in 
result3 ldap_result = self._ldap_call(self._l.result3,msgid,all,timeout)
File "/usr/lib/python2.7/dist-packages/ldap/ldapobject.py", line 96, in   
_ldap_call result = func(*args,**kwargs)
INVALID_CREDENTIALS: {'info': '80090308: LdapErr: DSID-0C0903A9, comment:   
AcceptSecurityContext error, data 52e, v1db1', 'desc': 'Invalid 
credentials'}

enter image description here

我可以使用我的cn(通用名称)属性对Active Directory进行身份验证吗?

感谢。

2 个答案:

答案 0 :(得分:1)

您不能在simple_bind()中使用cn值。请改为使用备用用户主体名称sAMAccountName@FQDN,或使用其他supported bind names之一。

答案 1 :(得分:0)

@marabu,谢谢你的回复。你是对的,不能在简单绑定中使用任何编辑器属性(如cn,....)。 在成功绑定后,我们只能通过搜索方法访问此属性。 在我的情况下,我有两个选择:

1)simple_bind_s(full_name,password)

2)simple_bind_s(sAMAccountName @ FQDN,密码)如果我们使用电子邮件地址进行会话身份验证

像这样:

enter image description here