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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28575359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:28:11  来源:igfitidea点击:

How to bind (authenticate) a user with ldap3 in python3

pythonpython-3.xldapldap3

提问by monkut

I'm trying to update some code to python3, using ldap3version '0.9.7.4'. (https://pypi.python.org/pypi/ldap3)

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

Previously, I used python-ldap with python2 to authenticate a user like this:

以前,我使用 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")

This properly returns (97, [], 2, [])on correct password, and raises ldap.INVALID_CREDENTIALSon a bind attempt using an incorrect password.

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

Using ldap3in python3 I'm doing the following:

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

It's raising the following exception:

它引发了以下异常:

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

I'm using the user_dnvalue returned by python2's ldap search, since this appears to be working in python2.

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

How can I get this to bind properly using ldap3 in python3?

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

(One thing strange, I noticed, is that the ldap3's LDAPInvalidCredentialsResult includes 'description': 'success'. I'm guessing this just means response successfully recieved...)

我注意到,奇怪的一件事是 ldap3 的 LDAPInvalidCredentialsResult 包括'description': 'success'。我猜这只是意味着成功收到响应......

回答by cannatag

I'm the author of ldap3, please set raise_exceptions=Falsein the Connection definition and check the connection.resultafter the bind. You should get the reason why your bind()is unsuccessful.

我是ldap3的作者,请raise_exceptions=False在连接定义中设置并检查connection.result绑定后。你应该知道你bind()失败的原因。