简体   繁体   English

python-ldap:我应该使用哪种编码来针对Microsoft AD检查密码?

[英]python-ldap: what encoding should I use to check password against Microsoft AD?

I have a django application for which i wrote an ActiveDirectoryAuthBackend , based on this snippet (it uses python-ldap). 我有一个django应用程序,我根据此代码段为其编写了ActiveDirectoryAuthBackend (它使用python-ldap)。

It works fine most of the time. 大多数情况下,它都能正常工作。

Now it happens that some users have non-ASCII characters in their domain passwords, which leads to authentication failure, because of an encoding coercion going bad in the simple_bind_s(username, password) function. 现在,由于某些用户在simple_bind_s(username, password)函数中的编码强制性simple_bind_s(username, password) ,因此某些用户的域密码中包含非ASCII字符,这会导致身份验证失败。

Actually, the password value that django passes is a unicode. 实际上,django传递的密码值是unicode。 So I guess I would need to encode this unicode before passing it to simple_bind_s , thus avoiding a failing default encoding translation. 因此,我想我需要对该unicode进行编码,然后再将其传递给simple_bind_s ,从而避免默认编码转换失败。

But I have no clue as for what encoding to use. 但是我不知道使用什么编码。 The password server is a Microsoft Active Directory. 密码服务器是Microsoft Active Directory。

Any idea? 任何想法?

Cheers. 干杯。

O. O.

After some tests and reading users' CN containing non ASCII characters, I've found that the most probable encoding for my domain is "latin-1". 经过一些测试并读取了包含非ASCII字符的用户CN,我发现我域中最可能的编码是“ latin-1”。

A good way to check credentials would look like: 一种检查凭据的好方法如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM