如何在python3中使用ldap3绑定(验证)用户

mon*_*kut 10 python ldap python-3.x ldap3

我正在尝试使用ldap3版本'0.9.7.4' 将一些代码更新为python3 .(https://pypi.python.org/pypi/ldap3)

以前,我使用python-ldap和python2来验证这样的用户:

import ldap
address = "ldap://HOST:389"
con = ldap.initialize(address)
base_dn = "ourDN=jjj"
con.protocol_version = ldap.VERSION3
search_filter = "(uid=USERNAME)"
result = con.search_s(base_dn, ldap.SCOPE_SUBTREE, search_filter, None)  
user_dn = result[0][0]  # get the user DN
con.simple_bind_s(user_dn, "PASSWORD")
Run Code Online (Sandbox Code Playgroud)

这将正确返回(97, [], 2, [])正确的密码,并ldap.INVALID_CREDENTIALS使用不正确的密码引发绑定尝试.

ldap3在python3中使用我正在做以下事情:

from ldap3 import Server, Connection, AUTH_SIMPLE, STRATEGY_SYNC, ALL
s = Server(HOST, port=389, get_info=ALL)
c = Connection(s, authentication=AUTH_SIMPLE, user=user_dn, password=PASSWORD, check_names=True, lazy=False, client_strategy=STRATEGY_SYNC, raise_exceptions=True)
c.open()
c.bind()
Run Code Online (Sandbox Code Playgroud)

它引发了以下异常:

ldap3.core.exceptions.LDAPInvalidCredentialsResult: LDAPInvalidCredentialsResult - 49 - invalidCredentials - [{'dn': '', 'message': '', 'type': 'bindResponse', 'result': 0, 'saslCreds': 'None', 'description': 'success', 'referrals': None}]
Run Code Online (Sandbox Code Playgroud)

我正在使用user_dnpython2的ldap搜索返回的值,因为这似乎在python2中工作.

如何在python3中使用ldap3正确绑定?

(有一点奇怪,我注意到,ldap3的LDAPInvalidCredentialsResult包括'description': 'success'.我猜这只是意味着响应成功收到了......)

can*_*tag 19

我是ldap3的作者,请raise_exceptions=False在Connection定义中设置并connection.result在绑定后检查.你应该知道你bind()不成功的原因.

  • 请在https://github.com/cannatag/ldap3/issues上打开一个问题,以便我可以跟踪问题.我需要知道您正在使用的python版本以及您尝试连接的LDAP服务器的版本. (2认同)