簡體   English   中英

python-ldap:我應該使用哪種編碼來針對Microsoft AD檢查密碼?

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

我有一個django應用程序,我根據此代碼段為其編寫了ActiveDirectoryAuthBackend (它使用python-ldap)。

大多數情況下,它都能正常工作。

現在,由於某些用戶在simple_bind_s(username, password)函數中的編碼強制性simple_bind_s(username, password) ,因此某些用戶的域密碼中包含非ASCII字符,這會導致身份驗證失敗。

實際上,django傳遞的密碼值是unicode。 因此,我想我需要對該unicode進行編碼,然后再將其傳遞給simple_bind_s ,從而避免默認編碼轉換失敗。

但是我不知道使用什么編碼。 密碼服務器是Microsoft Active Directory。

任何想法?

干杯。

O.

經過一些測試並讀取了包含非ASCII字符的用戶CN,我發現我域中最可能的編碼是“ 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM