繁体   English   中英

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

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

我正在尝试使用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' 。我猜这只是意味着成功收到响应......

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

确认您的 DN 不需要使用反斜杠\\转义逗号。

我的组织为用户提供了一个“姓,名”的 CN,所以我的 DN 需要是“CN=Doe\\, Jane, OU=xyz, ..., DC=abc, DC=com”

我通过使用Active Directory 资源管理器导航到我的用户对象,右键单击 > 查看属性以查看可分辨名称来实现这一点。 使用 AD Explorer 在其路径面包屑中显示的 DN 时,我遇到了此无效凭据错误,该 DN 忽略了转义字符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM