0

I'm new to python LDAP and need to do a search in LDAP, but

When the user's CN and DisplayName are different, I can only connect with Domain\user.

See below:

  • ldap3.Connection(s, user=user_cn, .... Failed,
  • ldap3.Connection(s, user=user_domain, .... Succeeded
>>> import ldap3
>>>
>>> ADDRESS = 'LDAP://192.168.26.10:389'
>>> user_cn = 'xxx test'
>>> user_domain = 'domain\xxx.test'
>>> password = 'password'
>>> s = ldap3.Server(ADDRESS, get_info=ldap3.ALL)
>>> c = ldap3.Connection(s, user=user_cn, password=password, auto_bind=True)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/ldap3/core/connection.py", line 325, in __init__
    self.do_auto_bind()
  File "/usr/local/lib/python3.5/dist-packages/ldap3/core/connection.py", line 353, in do_auto_bind
    raise LDAPBindError(self.last_error)
ldap3.core.exceptions.LDAPBindError: automatic bind not successful - invalidCredentials
>>> c.extend.standard.who_am_i()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'c' is not defined
>>>
>>> c = ldap3.Connection(s, user=user_domain, authentication = ldap3.NTLM,password=password, auto_bind=True)
>>> c.extend.standard.who_am_i()
'u:domain\\xxx.test'
>>>

It's ok to use domain/user to connect and bind(),
but when I do the search, I still need the CN in search_base.
Too much trouble to ask user's domain & CN & password, is there anyone can help me.
Thanks!

ldap3 = 2.6
Python = 3.5.2

C.K.
  • 4,348
  • 29
  • 43
  • The following are [30 code examples](https://www.programcreek.com/python/example/107944/ldap3) for showing how to use `ldap3` and [Tutorial: Introduction to ldap3](https://ldap3.readthedocs.io/en/latest/tutorial_intro.html#tutorial-introduction-to-ldap3). – Milovan Tomašević Jan 26 '21 at 19:12

1 Answers1

1

You can always try to change user=user_cn(user_domain) with just:

user="{}\\{}".format("domain", username)

At least in my version this is how I was able to solve this:

    conn1 = Connection(Server('LDAP://xxxxx.xxxx.xxxx.com:389'),
                       auto_bind=True,
                       user="{}\\{}".format("domain", username),
                       password=password)
Ivan Ivanov
  • 131
  • 1
  • 2
  • 6
  • Thanks, I still have a problem, my user ≠ "{}\\{}".format("domain", username). My user CN=(e.g. 'xxx text') while my user = domain\xxx.text (and could be some others). I don't know why they have such a difference. Is it normal? thanks again. – C.K. Feb 23 '20 at 21:56