Search code examples
pythonldapldap3

LDAP filter with ldap3 library not working


I am trying to retrieve information from a user from LDAP with the following Python code snippet. I am using the ldap3 library to query LDAP.

def searchUser():
    server = Server('myLdapServer:389')
    try:
        with Connection(server) as conn:
            conn.search('ou=xuser,ou=xxx,o=com', '(attribute=cn=user,ou=yuser,ou=xxx,o=com)', attributes=['*'])
            for entry in connection.entries:
                print(entry)
    except:
        return 'error in ldap search'

Unfortunately, I am not getting any result with this query even though when I enter the Ldap-Base and Ldap-Filter to the Softerra Ldap-Browser I am getting a result. I am using the same Ldap user with the Softerra-Browser and in the code.


Solution

  • Ok found the solution. Had to provide Credentials in the Connection like so:

    with ldap3.Connection(server, user="myLdapUser", password="myLdapPassword", auto_bind=True)