PythonLDAP:我应该使用什么编码来对照Microsoft AD检查密码?
我有一个django应用程序,我基于它编写了一个PythonLDAP:我应该使用什么编码来对照Microsoft AD检查密码?,python,active-directory,ldap,Python,Active Directory,Ldap,我有一个django应用程序,我基于它编写了一个ActiveDirectoryAuthBackend(它使用python ldap) 它大部分时间都很好用 现在,有些用户的域密码中有非ASCII字符,这会导致身份验证失败,因为simple\u bind_s(username,password)函数中的编码强制变差了 实际上,django传递的密码值是unicode。因此,我想我需要先对这个unicode编码,然后再将其传递给simple\u bind\u s,从而避免默认编码转换失败 但我不知道
ActiveDirectoryAuthBackend
(它使用python ldap)
它大部分时间都很好用
现在,有些用户的域密码中有非ASCII字符,这会导致身份验证失败,因为simple\u bind_s(username,password)
函数中的编码强制变差了
实际上,django传递的密码值是unicode。因此,我想我需要先对这个unicode编码,然后再将其传递给simple\u bind\u s
,从而避免默认编码转换失败
但我不知道该用什么编码。密码服务器是Microsoft Active Directory
有什么想法吗
干杯
O.经过一些测试和阅读用户的CN(包含非ASCII字符)后,我发现我的域最可能的编码是“latin-1” 检查凭据的好方法如下所示:
class ActiveDirectoryAuthBackend(backends.ModelBackend):
def authenticate(self, username=None, password=''):
try:
from django.contrib.auth.model import User
user = User.objects.get(username=username):
except User.DoesNotExist:
return None
from django.conf import settings
import ldap
con = ldap.initialize('ldap://{0}'.format(settings.get('LDAP_SERVER', None)))
con.set_option(ldap.OPT_REFERRALS, 0)
try:
con.simple_bind_s(username, password.encode('latin1'))
return user
exceot ldap.INVALID_CREDENTIALS:
return None