django-auth-ldap failed authentication

余生颓废 提交于 2019-12-05 23:06:30

I found the answer.

I changed the AUTH_LDAP_BIND_DN by adding (OU=Users)

I must use samAccountName instead of CN in AUTH_LDAP_USER_SEARCH

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

My views.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()

    ....

Hope this help all :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!