簡體   English   中英

django 通過 LDAP 進行用戶身份驗證

[英]django User authentication via LDAP

我正在嘗試通過 ldap 針對我們的 AD 檢查我的 django 項目中的用戶登錄。 我在網上找到了很多我到目前為止嘗試過的教程。

由於某種原因, authenticate(username, password) -函數返回None

到目前為止,這是我的代碼:

views.py(登錄)

def login_view(request):
    if not request.user.is_authenticated:
        if request.method == 'POST':
            login_form = Login_form(request.POST)
            if login_form.is_valid():
                username = login_form.data.get('username')
                password = login_form.data.get('password')
                domain_name = "@my.domain.com"
                if domain_name not in username:
                    username += domain_name
                try:
                    user = authenticate(username=username, password=password)
                    print(user) # this gives me None
                    if user is not None:
                        if user.is_active:
                            login(request=request, user=user)
                            return redirect('index')
                    else:
                        form = AuthenticationForm()
                        messages.error(request, 'Try again!')
                        return render(request, 'myapp/login.html', {'form': form})
                except ldap.LDAPError as e:
                    print(e) # no error is displayed here
                    form = AuthenticationForm()
                    messages.error(request, 'Try again!')
                    return render(request, 'myapp/login.html', {'form': form})
          ### Some more funcs to  
          ### redirect to login.html
          ### if the login fails

設置.py:

AUTHENTICATION_BACKENDS = (
    'django_auth_ldap.backend.LDAPBackend',
)

AUTH_LDAP_SERVER_URI = "ldap://my.domain.com:389"
AUTH_LDAP_BIND_DN = "CN=Users,DC=my,DC=domain,DC=com"
AUTH_LDAP_BIND_PASSWORD = ""    # I tried with blank password for anonymous bind or
                                # with "%(password)s" as template but I don't know if that's possible
                                # and also without the AUTH_LDAP_BIND_PASSWORD setting
AUTH_LDAP_CONNECTION_OPTIONS = {ldap.OPT_REFERRALS: 0}
AUTH_LDAP_USER_ATTR_MAP = {'group': "memberof", "first_name": "givenName", "last_name": "sn"}
AUTH_LDAP_USER_SEARCH = LDAPSearch("DC=my,DC=domain,DC=com,CN=Users",
                                   ldap.SCOPE_SUBTREE, "(sAMAccountName=%(user)s)") 
# since we need to login via username i need to search for username@my.domain.com 
# so I try to search for sAMAccountName

不久前,我在 PHP 中編寫了一個 LDAP 登錄腳本,其工作原理類似於魅力,所有 DN、綁定和搜索都是相同的。

所以我的問題是:

哪里出了問題或者我錯過了什么?

我強烈推薦使用django-python3-ldap 在嘗試其他產品多年后,我們在生產中使用了這個 package 多年,它可以工作,並且完全用 Python 3 編寫: httpspython: //3-ldap/djangoetian/github-ldap/

我們在端口 636 和ldaps上使用它,它也能正常工作。

它使我們不必編寫自己的自定義后端或登錄方法; 我們所要做的就是更改一些設置並寫入format_username function。 自述文件有關於連接到 Active Directory 的很好信息:我將從該配置開始,看看它是如何工作的。 祝你好運!

暫無
暫無

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

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