Django Auth未在LDAP

时间:2016-04-20 07:09:39

标签: django authentication ldap django-views

上午,

我在我的proyect中实现了Django Auth Ldap,但它无效。我检查了ldap连接(通过Django shell)并返回一个搜索,所以我猜python-ldap正在工作。我用了下一个:

import ldap
con = ldap.initialize("ldap://hostname")
con.simple_bind_s( "CN=MyName MySurname, CN=Users, DC=CompanyName, DC=local", "MyPassword" )
con.search_s( 'DC=CompanyName, DC=local', ldap.SCOPE_SUBTREE, '(objectclass=person)', ['sn'] )

当我尝试通过Web验证用户时(使用Django-Auth-Ldap),身份验证始终返回None。

Settings. (LDAP Configuration).
AUTH_LDAP_SERVER_URI = "ldap://hostname"
AUTH_LDAP_BIND_DN = "CN=MyName MySurname, CN=Users, DC=CompanyName, DC=local"
AUTH_LDAP_BIND_PASSWORD = "MyPassword"
AUTH_LDAP_USER_SEARCH = LDAPSearch("CN=Users, DC=CompanyName, DC=local", ldap.SCOPE_SUBTREE, "(uid=%(user)s)")
AUTH_LDAP_CONNECTION_OPTIONS = {
    ldap.OPT_REFERRALS: False
}

from django_auth_ldap.backend import LDAPBackend
View.
def Login(request):
 usr = "MyUserName"
 pwd = "MyPassword"

    if request.method == 'POST':
        ldap_backend = LDAPBackend()
        user = ldap_backend.authenticate(usr, pwd)
        print user
        print usr, pwd

在我看来,我传递了ldap身份验证我的用户和密码,我用它来登录域名。那是对的吗? 我从Directory Active server中的命令获得值"CN=MyName MySurname, CN=Users, DC=CompanyName, DC=local",类似于: dsquery user

这是AD架构: AD Structure

我做错了什么?

谢谢你们。

编辑:问题是当我定义搜索时,如果我将其定义为 AUTH_LDAP_USER_SEARCH = LDAPSearch(“CN = Users,DC = CompanyName,DC = local”,ldap.SCOPE_SUBTREE,“(CN = %(用户)s)“)正在运行(并且,在视图中,我必须传递为 usr =”MyNameMySurname“。)。如何通过用于登录的用户名定义搜索。

1 个答案:

答案 0 :(得分:0)

最后......我必须使用samAccountName而不是CN。我希望它能帮到你们所有人。多谢你们。

相关问题