我目前正在尝试使用 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)
(我没有这个脚本)
关于Python LDAP 更改密码没有效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55144501/