簡體   English   中英

Django Python-Ldap身份驗證

[英]Django Python - Ldap Authentication

我目前在Django Python上工作。 我的目的是從Ldap目錄中對用戶進行身份驗證。 我確實有python代碼來訪問ldap目錄並檢索信息。

代碼:

import ldap
try:
        l = ldap.open("ldap.forumsys.com")
        l.protocol_version = ldap.VERSION2
        username = "cn=read-only-admin,dc=example,dc=com"
        password = "password"
        l.simple_bind(username,password)

except ldap.LDAPError,e:
        print e

我的疑問是,我將如何在我的django中實現它? 如何在Django中使用這些代碼並實現它?

提前致謝

幾乎是相同的,您通常需要在ldap(通常是用戶sam accountName)上輸入有關ldap的特定搜索時,通常是在來自用戶登錄名的提交調用之后。

userDN = ""
passwordUser = ""
base_dn = 'node where we start to seach, from your AD structure'
#attrs = ['description', 'telephoneNumber', 'title', 'mail' , 'lastLogon', 'memberOf', 'accountExpires',]
attrs = []

def myAccount(request):
    con = ldap.initialize("ldap://ldapserver")
    con.simple_bind_s( userDN, passwordUser )
    filter = '(sAMAccountName=' + "loginName" + ')'
    user = con.search_s( base_dn, ldap.SCOPE_SUBTREE, filter, attrs )
    con.unbind()

    userInfoList = []
    for key, value in user[0][1].items():
        userInfoList +=  [userInfo(key, value)]

    return render_to_response('template.html',{'userInfoList':userInfoList, 'dnUser': user[0][0]}, context_instance = RequestContext(request))

在下面的代碼中,一個特定的模板正在調用myAccount方法,在此方法中,我們使用ldap補碼通過DN中授權的用戶通過LDAP進行搜索。 之后,我們將恢復通過此搜索獲得的信息。

希望能幫助到你。 您可能有任何疑問,請告訴我:)

暫無
暫無

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

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