【发布时间】:2022-12-14 14:49:30
【问题描述】:
我正在尝试通过 .NET 核心 Web API 重置活动目录用户密码但总是返回异常以下,即使我输入了非常复杂的密码
System.DirectoryServices.AccountManagement.PasswordException: '密码不符合密码策略要求。 检查最小密码长度,密码复杂度 和密码历史要求。 (0x800708C5)'
我尝试了两种方法(
DirectoryEntry和新方法),但我遇到了同样的异常。这是我的代码,但我认为
public bool ResetPassword(string oldPassword, string newPassword, string userNameI) { /* // set up domain context PrincipalContext context = new PrincipalContext(ContextType.Domain, LDAP_PATH, userName, password); if (context != null) { // find the user you want to delete UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userNameI); if (user != null) { user.Enabled = true; user.ChangePassword(oldPassword,newPassword); user.ExpirePasswordNow(); user.Save(); return true; } }*/ /* var entry = new DirectoryEntry { Path = "LDAP://MyIP", Username = userName, Password = password }; using (var searcher = new DirectorySearcher(entry)) { searcher.Filter = "(SAMAccountName=" + userNameI + ")"; var result = searcher.FindOne(); var user = result.GetDirectoryEntry(); user.Invoke("ChangePassword", new object[] { oldPassword.Trim(), newPassword.Trim() }); user.CommitChanges(); return true; } */ using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "LDAPIP", userName, password)) { using (UserPrincipal user = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, userNameI)) { if (user != null) { user.ChangePassword(oldPassword, newPassword); user.Save(); return true; } else { throw new Exception(string.Format("Username not found: {0}", userNameI)); } } return false; } }
【问题讨论】:
-
如果您没有设置任何策略,则密码遵循默认策略,您修改的密码需要遵循该策略。请查看此文档:Azure AD password policies。
-
我无法通过代码设置的相同密码我可以在服务器上手动设置,所以我认为问题与密码策略无关,我猜出现的异常不准确。
-
您是否添加了断点以查看更改后的密码是否符合您的预期?也许这不是您经过一些处理后设置的密码样式。
-
是的,我做到了,我尝试使用 validCredintioal 和提供的密码,它工作正常,只有重置密码的问题
-
请注意,您是在“更改”密码,而不是“重置”密码。重置 (
SetPassword) 是一项管理任务,您可以在不知道旧密码的情况下设置密码。
标签: asp.net-core .net-core active-directory directoryentry userprincipal