2016-07-27 108 views
1

我正在使用一個簡單的表單,它接受用戶名和密碼並嘗試使用ldap服務器對該用戶進行身份驗證。儀表板的實際門戶是用php編寫的,但我必須使用django(和pyldap)重寫它。在我的settings.py文件中,我有以下設置(我修改了URI,因爲我正在使用的公司使用該URI)。無法使用Django和ldap進行身份驗證

# LDAP Settings 
# Set backends 
AUTHENTICATION_BACKENDS = (
    'django_auth_ldap.backend.LDAPBackend', 
) 

# LDAP Server 
AUTH_LDAP_SERVER_URI = 'ldaps://eddie.xy.zzzzz.com:3268' 
AUTH_LDAP_START_TLS = True 

# Search settings 
AUTH_LDAP_BIND_DN = "" 
AUTH_LDAP_BIND_PASSWORD = "" 
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=users,dc=xy,dc=zzzzz,dc=com", ldap.SCOPE_SUBTREE, "(uid=%(user)s)") 

在我的views.py,我有以下代碼(只是爲了測試的東西)

from django.shortcuts import render 
from django.contrib.auth import authenticate 
from django_auth_ldap.backend import LDAPBackend # Greyed out 

def login(request): 
    if request.method == 'POST': 
     # Get info from POST request 
     usr = request.POST['username'] 
     pas = request.POST['password'] 

     # Authenticate the user using provided data 
     user = authenticate(username=usr, password=pas) 
     print(str(user)) 

     if user is not None: 
      return render(request, 'dashboard/index.html', {}) 
     else: 
      print("The username and password were incorrect.") 
      return render(request, 'dashboard/error.html', {}) 

    elif request.method == 'GET': 
     return render(request, 'dashboard/login.html', {}) 

的login.html包含在它的表單一個簡單的HTML代碼,用方法=「後「action =」/ login /「。 Index.html包含一個簡單的「成功」消息,而error.html包含一個簡單的「錯誤!」信息。我以我製作的表格使用我的(工作)憑證,但我無法獲得成功消息。

我也試過,是這樣的:

at = LDAPBackend() 
user = authenticate(username=usr, password=pas) 

,而不是在上面的代碼中使用在認證方法。不幸的是,我得到了同樣的行爲。我錯過了什麼,我的錯誤在哪裏?

回答

0

Finnaly,我設法做到了。我不得不設置這個變量:

AUTH_LDAP_USER_DN_TEMPLATE = "%(user)s" 

現在它的工作完美。我希望這可能會幫助其他人。不管怎麼說,還是要謝謝你!

祝您有美好的一天!