Django Ldap自动验证仅适用于DN_模板
尝试使用LDAP服务器进行Django身份验证,但我无法登录,我使用AUTH_LDAP_USER_DN_模板实现了这一点,但使用LDAPSearchUnion(我在实际项目中需要的)添加这两个ou只会显示“不正确的用户名/密码”(登录失败时的消息) 以下是我的设置.py:Django Ldap自动验证仅适用于DN_模板,django,python-3.x,ldap,django-auth-ldap,Django,Python 3.x,Ldap,Django Auth Ldap,尝试使用LDAP服务器进行Django身份验证,但我无法登录,我使用AUTH_LDAP_USER_DN_模板实现了这一点,但使用LDAPSearchUnion(我在实际项目中需要的)添加这两个ou只会显示“不正确的用户名/密码”(登录失败时的消息) 以下是我的设置.py: AUTH_LDAP_SERVER_URI = "ldap://ldap.forumsys.com" AUTH_LDAP_BIND_DN = "cn=read-only-admin,dc=example,dc=com" AUT
AUTH_LDAP_SERVER_URI = "ldap://ldap.forumsys.com"
AUTH_LDAP_BIND_DN = "cn=read-only-admin,dc=example,dc=com"
AUTH_LDAP_BIND_PASSWORD = "password"
AUTH_LDAP_USER_SEARCH = LDAPSearchUnion(
LDAPSearch("ou=mathematicians,dc=example,dc=com",
ldap.SCOPE_SUBTREE, "(uid=%(user)s)"),
LDAPSearch("ou=scientists,dc=example,dc=com",
ldap.SCOPE_SUBTREE, "(uid=%(user)s)"),
)
AUTH_LDAP_USER_ATTR_MAP = {
'first_name': 'givenName',
'last_name': 'sn',
'email': 'mail',
}
AUTH_LDAP_ALWAYS_UPDATE_USER = True
AUTHENTICATION_BACKENDS = (
'django_auth_ldap.backend.LDAPBackend',
)
Views.py:
def login_page(request):
if request.user.is_authenticated:
return redirect("list")
context = {
'form': LoginForm,
}
if request.method == "POST":
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data.get("username")
password = form.cleaned_data.get("password")
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('list')
else:
messages.error(request, 'Wrong username or password')
return redirect("login")
return render(request, 'login.html', context)
有人知道我做错了什么吗
编辑
我通过以下方法成功地做到了这一点:
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=dc=example,dc=com",
ldap.SCOPE_SUBTREE, "(uid=%(user)s)")
但是这个想法是添加一个OU(例如科学家),这样只有科学家才能登录到这个应用程序