Python LDAP 更改密码没有效果

标签 python python-3.x ldap passwords

我目前正在尝试使用 Python (3) 和 LDAP 模块修改 AD 上用户的密码。 当我的脚本完成时,一切看起来都很顺利。 但是,密码与以前相同。

这是我的脚本:

LDAP_SERVER = <domain>
LDAP_USERNAME = <admin_username>
LDAP_PASSWORD = <admin_password>
dn = <DN>
quoted_new_password = '\"' + <new_password> + '\"'
quoted_new_password_bytes = quoted_new_password.encode('UTF-16LE')

ldap_client = ldap.initialize(LDAP_SERVER)
ldap_client.set_option(ldap.OPT_REFERRALS, 0)
ldap_client.simple_bind_s(LDAP_USERNAME, LDAP_PASSWORD)
ad_user_filter = '(&(objectClass=user)(sAMAccountName=<username-for-password-modification>))'
res = ldap_client.search_s(dn, ldap.SCOPE_SUBTREE, ad_user_filter)
user_dn = (res[0][1]['distinguishedName'][0]).decode("utf-8")
modlist = [ (ldap.MOD_REPLACE, "userPassword", quoted_new_password_bytes)]
ldap_client.modify_s(user_dn, modlist)

结果是一个像这样的元组

(<number>, [], <number>, [])

然后,当我尝试连接到 AD(具有相同域)时,旧密码有效,但新密码无效。

我是不是忘记了什么?

编辑:当我输入空字符串作为新密码时,结果是相同的,即使我的 AD 需要至少 14 个字符。

编辑:“modify_s”的最后结果是

(103, [], 3, [])

但是,103代码不对应任何内容...

最佳答案

已解决

域名为ldap://the_domain:389。 但它无法工作,因为我必须使用安全服务器:ldaps 而不是 ldap,端口 636 而不是 389。

所以我将 LDAP_SERVER 更改为 ldaps://the_domain:636

但是,我的脚本不再起作用了。 我在改编之前从另一篇文章中获取了这个脚本:

import ldap3

SERVER = 'ldaps://thedomain:636'
BASE_DN = "DC=domain,DC=com"
LDAP_USERNAME = "admin_username@thedomain.com"
LDAP_PASSWORD = "admin_password"
CURRENT_PWD = "the_current_password"
NEW_PWD = "the_new_password"
SEARCHED_USERNAME = "M_tete_en_l_air"

SEARCH_FILTER = '(&(objectClass=User)(samaccountname='+SEARCHED_USERNAME +'))'

USER_DN = ""

ldap_server = ldap3.Server(SERVER, get_info=ldap3.ALL)
conn = ldap3.Connection(ldap_server, LDAP_USERNAME, LDAP_PASSWORD, auto_bind=True)
conn.start_tls()

conn.search(search_base = BASE_DN,
         search_filter = SEARCH_FILTER,
         search_scope = ldap3.SUBTREE,
         attributes = ['cn', 'givenName'],
         paged_size = 5)

for entry in conn.response:
    if entry.get("dn") and entry.get("attributes"):
        if entry.get("attributes").get("cn"):
            USER_DN=entry.get("dn")

print(USER_DN)
success = ldap3.extend.microsoft.modifyPassword.ad_modify_password(conn, USER_DN, NEW_PWD, CURRENT_PWD,  controls=None)
print("Password modified: ", success)

(我没有这个脚本)

Source (StackOverflow answer)

关于Python LDAP 更改密码没有效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55144501/

相关文章:

python - 来自 python 脚本的最小可能的 Windows 可执行文件

python - 当用户仅单击输入时,我试图停止循环?

python - 如何在 Windows 上正确运行 Google App Engine Web 服务器?

python - 用于控制多个远程主机的 GUI(使用相同的 GUI 进行模拟和真实硬件)

每次我添加新行时 Python 文件都会覆盖

python - 优化器返回 None

python - __instancecheck__ - 覆盖显示无效 - 我做错了什么?

当 ldap.OPT_X_TLS_REQUIRE_CERT 设置为 ldap.OPT_X_TLS_NEVER 时出现 Python LDAP TLS 错误

java - LDAP 查询中的 NamingEnumeration 不返回 null

Java JNDI - 在 Microsoft LDAP 中更改用户密码时的限制 - 身份验证异常