如何强制用户在下次使用 ldap3 python 模块登录时更改密码?

How to force user to change password at next logon with ldap3 python module?

我正在使用 ldap3 通过 python 脚本在 Active Directory (Win 2012R2) 中创建用户帐户。我无法设置的唯一一个属性是 "User must change password at next logon"。您能否建议一种在使用创建后立即标记此复选框的方法?我尝试更改 UserAccountControl 和 pwdLastSet 属性但没有运气(

-1 是唯一一个有效参数

password_expire = {"pwdLastSet": (MODIFY_REPLACE, [-1])}
connect.modify(dn=user_dn, changes=password_expire)

PASSWORD_EXPIRED 0x800000 8388608

password_expire = {"UserAccountControl": (MODIFY_REPLACE, [8388608])}
connect.modify(dn=user_dn, changes=password_expire)

注意:下面给出的解决方案 可能,仅在即将发布的版本之后有效LDAP3 库的发布 (v2.5)。目前,我不知道可以为 OP 提供所需解决方案的解决方法。

在此处查看 changelog,其中列出了:

For the release v2.5, pwdLAstSet in AD is valid for 0 and -1.

---It has not been released for now, and just commented (thanks to Anton Belov for notifying).


如果 pwdLastSet 的值设置为 0,并且 UAC 属性不包含标志 UF_DONT_EXPIRE_PASSWD,用户将在下次登录时被要求更改密码。查看 Pwd-Last-Set attribute here on MSDN 了解更多信息。

按照上面的建议修改您的代码将在用户帐户的复选框中显示用于更改密码的勾号。

仅使用您的第一个代码,并将值设置为 0,如下所述。

password_expire = {"pwdLastSet": (MODIFY_REPLACE, [-1])}  # // use 0 instead of -1.
connect.modify(dn=user_dn, changes=password_expire)
# // you don't need to play with UserAccountControl further...