【发布时间】:2017-06-03 23:42:34
【问题描述】:
我正在尝试使用 django 中的 LDAP 服务器对用户进行身份验证。
我的 settings.py 配置如下:
AUTH_LDAP_SERVER_URI = "ldap.forumsys.com"
AUTH_LDAP_BIND_DN = "cn=read-only-admin,dc=example,dc=com"
AUTH_LDAP_BIND_PASSWORD = "password"
AUTH_LDAP_USER_SEARCH = LDAPSearch("dc=example,dc=com",
ldap.SCOPE_SUBTREE, "(uid=%(user)s)")
AUTH_LDAP_START_TLS = True
AUTHENTICATION_BACKENDS = (
'django_auth_ldap.backend.LDAPBackend',
'django.contrib.auth.backends.ModelBackend',
)
在我看来,我尝试使用 LDAPBACKEND 对其进行身份验证
from django.http import HttpResponse
from django_auth_ldap.backend import LDAPBackend
from django.contrib.auth.models import User
from django.conf import settings
def login_user(request):
state = ""
username = settings.AUTH_LDAP_BIND_DN
password = settings.AUTH_LDAP_BIND_PASSWORD
auth = LDAPBackend()
try:
User = auth.authenticate(username=username,password=password)
if User is not None:
state = "Valid"
else:
state = "Invalid"
except LDAPError as e:
state = "Error"
return HttpResponse(state)
但我收到一个错误
验证 cn=read-only-admin,dc=example,dc=com 时出现 LDAPError: LDAPError(0,'错误')
我还有另一个疑问。 username 和 password 是否与 bind_username 和 bind_password 相同?
【问题讨论】:
标签: python django python-2.7 authentication ldap