Search code examples
pythonldappython-ldap

How to find Active Directory user by First Name


After successfully connecting to the Active Directory running on Windows Server with 10.15.120.250 ip address using python-ldap:

import ldap
url = 'ldap://10.15.120.250'
user = 'myusername@post.ad'
password = 'pass'
conn = ldap.initialize(url)
conn.protocol_version = 3
conn.set_option(ldap.OPT_REFERRALS, 0)
conn.simple_bind_s(user, password) 

I want to go ahead and find a user with a last name "Johnson".

How to achieve it?


Solution

  • The last name is typically stored in the "sn" (surname) field. http://www.zytrax.com/books/ldap/ape/

    conn.search_ext('cn=base,dc=example,dc=com', ldap.SCOPE_SUBTREE, '(sn=Johnson)')
    

    The first name is in "givenName"

    conn.search_ext('cn=base,dc=example,dc=com', ldap.SCOPE_SUBTREE, '(givenName=Johnson)')