Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PythonLDAP:我应该使用什么编码来对照Microsoft AD检查密码?_Python_Active Directory_Ldap - Fatal编程技术网

PythonLDAP:我应该使用什么编码来对照Microsoft AD检查密码?

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,从而避免默认编码转换失败 但我不知道

我有一个django应用程序,我基于它编写了一个
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