Django Python - Ldap认证 [英] Django Python - Ldap Authentication

查看:294
本文介绍了Django Python - Ldap认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在Django Python上工作。我的目标是从Ldap目录验证用户。我有我的python代码访问ldap目录并检索信息。



代码:

  import ldap 
try:
l = ldap.open(ldap.forumsys.com)
l.protocol_version = ldap.VERSION2
username =cn =只读管理员,dc =示例,dc = com
password =password
l.simple_bind(username,password)

除了ldap.LDAPError,e:
打印e

我的疑问是,我将如何在我的django中实现? 。在django中如何使用这些代码并实现它?



提前感谢

解决方案

几乎一样,你通常来自用户的登录后,通常来自用户的登录后,您需要编写关于ldap(sam accountName)的具体搜索。

  userDN =
passwordUser =
base_dn ='我们开始浏览的节点,从你的AD结构'
#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 = []
用于键值,用户[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补码来进行LDAP搜索,由DN授权的用户进行搜索。之后,我们恢复通过此搜索获得的信息。



希望它有帮助。有任何疑问,你可以拥有,只是让我知道:)


I am currently work on Django Python . My aim to authenticate the user from the Ldap directory . I do have my python code to access the ldap directory and retrieve the information.

Code :

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

My doubt is that , how would i implement this in my django? . How do use these code in django and to implement it?

Thanks in advance

解决方案

It is almost the same, you have to code your specific search about ldap (sam accountName usually) when you want it, normally after submit call coming from a user´s login.

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))

In code below, an specific template is calling to myAccount method, in this method we use ldap complement for doing a search over LDAP, by an user authorized in DN for doing searches. After that we recover the info obtained by this search.

Hope it helps. Any doubt you could have, just let me know :)

这篇关于Django Python - Ldap认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆