DirectoryServices UserPrincipal.SetPassword忽略密码策略(密码历史记录)
正如标题所示,我在设置用户密码时遇到了关于尊重密码策略的问题,特别是密码历史记录限制。
当用户不知道他当前的密码时,该方案是用户密码重置。 我使用以下内容来完成此任务:
using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "XXXX", "ADMINUSER", "ADMINPASSWORD")) { using (UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username)) { user.SetPassword(password); } }
这适用于每个策略MINUS密码历史记录限制。
现在采取这种情况,当用户想要更改他们的密码并知道我正在使用的当前密码时:
using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "XXXX.XXX.com")) { using (UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username)) { user.ChangePassword(currentPassword, newPassword); } }
…按预期工作,并validation所有密码策略限制。
有没有人不得不这样做?
干杯:)
这是设计,据我所知。 SetPassword意图像管理员一样重置用户密码 – 复杂性策略成立但对历史没有限制。 假设管理员重置您的密码,看到“无法设置相同的密码” – 您的一个密码被泄露。
我们的解决方法是允许管理层仅通过我们的一个Web子系统并保留哈希的历史记录,以便validation历史记录的责任放在自定义子系统而不是广告上。
如果通过FindByIdentify找不到用户名,您可能还想先将其检查为null!
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "XXXX.XXX.com")) { using (UserPrincipal user = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, username)) { if (user != null) { user.ChangePassword(currentPassword, newPassword); } else { throw new Exception(string.Format("Username not found: {0}", username)); } } }