我对更改用户AD密码的c#脚本有疑问,当尝试更改密码时,他们会抛出异常
A constraint violation occurred. (Exception from HRESULT: 0x8007202F)
代码
DirectoryEntry entry = new DirectoryEntry("LDAP://domain.com", strLoginName, oldpassword.Text.ToString(), AuthenticationTypes.Secure);
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + strLoginName + ")";
search.SearchScope = SearchScope.Subtree;
search.CacheResults = false;
SearchResultCollection results = search.FindAll();
foreach (SearchResult result in results)
entry = result.GetDirectoryEntry();
entry.Invoke("ChangePassword", new object[] { oldpassword.Text.ToString(), newpassword.Text.ToString() });
entry.CommitChanges();
哪个可能有问题?
答案 0 :(得分:3)
请看这里:https://stackoverflow.com/a/1066177/1027551
对于您的示例,它看起来像这样:
using (var context = new PrincipalContext(ContextType.Domain,"domain.com"))
using (var user = UserPrincipal.FindByIdentity( context, IdentityType.SamAccountName,
strLoginName ))
{
user.ChangePassword( oldPassword, newpassword );
}
我希望有所帮助。