2

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

1

1 Answer 1

1

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

3
  • Thanks for the ans . I have implemented it the above way . But i want to implement the authentication , using the default Django-Ldap settings . How can it be done ? Commented Jan 18, 2017 at 4:52
  • As far as I know, Django hasn´t ldap settings by default. You have to install django-ldap module. Commented Jan 18, 2017 at 7:55
  • By the way, may you reward my efford and rate my answer? Thanks mate. :) Commented Jan 18, 2017 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.