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

2024-04-19 01:45:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用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")

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

在python3中使用ldap3我正在执行以下操作:

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

它引发了以下异常:

ldap3.core.exceptions.LDAPInvalidCredentialsResult: LDAPInvalidCredentialsResult - 49 - invalidCredentials - [{'dn': '', 'message': '', 'type': 'bindResponse', 'result': 0, 'saslCreds': 'None', 'description': 'success', 'referrals': None}]

我正在使用python2的ldap搜索返回的user_dn值,因为这似乎在python2中起作用。

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

有一件事很奇怪,我注意到,ldap3的LDAPInvalidCredentialsResult包含'description': 'success'。我猜这只是意味着成功地收到了回应。。。


Tags: importpypinonehostsearchaddressresultldap