C#活动目录调用";ChangePassword";无法联系域
本文关键字:quot 联系 ChangePassword 活动 调用 | 更新日期: 2023-09-27 18:03:42
我有以下代码作为web应用程序的一部分,让我的Active Directory用户能够更新他们的密码(同时用于Active Directory和gmail(。我正在将C#与System.DirectoryServices.AccountManagement.一起使用
这个代码一直工作到昨天
try
{
State.log.WriteLine("Connecting LDAP.");
string ldapPath = "LDAP://192.168.76.3";
DirectoryEntry directionEntry = new DirectoryEntry(ldapPath, domainName + "''" + userName, currentPassword);
if (directionEntry != null)
{
DirectorySearcher search = new DirectorySearcher(directionEntry);
State.log.WriteLine("LDAP Connected, searching directory for SAMAccountName");
search.Filter = "(SAMAccountName=" + userName + ")";
SearchResult result = search.FindOne();
if (result != null)
{
State.log.WriteLine("Getting User Entry.");
DirectoryEntry userEntry = result.GetDirectoryEntry();
if (userEntry != null)
{
State.log.WriteLine("Setting Password");
if (force)
{
userEntry.Invoke("SetPassword", new[] { newPassword });
}
else
{
userEntry.Invoke("ChangePassword", new object[] { currentPassword, newPassword });
}
userEntry.CommitChanges();
State.log.WriteLine("Changes Committed to ActiveDirectory.");
}
else
{
State.log.WriteLine("Could not get user Entry...");
}
}
else
{
State.log.WriteLine("Search returned no results.");
}
}
else
{
State.log.WriteLine("Could not connect to LDAP with given username and passwd");
}
}
从昨天开始,这段代码进入了以下行:
userEntry.Invoke("ChangePassword", new object[] { currentPassword, newPassword });
然后抛出以下异常:
[8:37:00AM]:符合密码要求。
[8:37:00AM]:正在连接LDAP。
[8:37:00AM]:LDAP已连接,正在目录中搜索SAMAccountName
[8:37:01AM]:正在获取用户条目。
[8:37:01AM]:设置密码
[8:37:01AM]:重置jason的Windows密码失败。
调用的目标引发了异常。
系统无法联系域控制器来为身份验证请求提供服务。请稍后再试。(HRESULT:0x800704F1的异常(
";"力";选项使用";设置密码";仍然工作得很好,但;ChangePassword";可以由非管理员用户调用的方法不能。
更改userPrincipal.ChangePassword("旧通行证"、"新通行证"(;至userPrincipal.SetPassword(model.NewPassword);
我找到了一个变通方法,忘记发布了。我所做的是使用上面的代码来验证用户,然后只调用我的"ForceChangePassword"方法:
public static void ForceChangeADPassword(String username, String newPassword)
{
String DN = "";
try
{
DN = GetObjectDistinguishedName(objectClass.user, returnType.distinguishedName, username, DOMAIN_CONTROLLER_IP);
}
catch(Exception e)
{
throw new PasswordException(String.Format("Could not find AD User {0}", username), e);
}
if(DN.Equals(""))
throw new PasswordException(String.Format("Could not find AD User {0}", username));
DirectoryEntry userEntry = new DirectoryEntry(DN.Replace("LDAP://", LdapRootPath), "accounts", AcctPwd);
userEntry.Invoke("SetPassword", new object[] { newPassword });
userEntry.Properties["LockOutTime"].Value = 0;
userEntry.CommitChanges();
userEntry.Close();
}
本月早些时候,微软发布了一个安全补丁,解决了密码更改领域的一些漏洞。具体而言,在更改密码时Kerberos身份验证失败后,更新阻止了NTLM身份验证的回退。
您可能想在此处阅读有关更新的更多信息。
Microsoft更新了这篇文章:https://support.microsoft.com/en-us/kb/3177108。在这里,他们向我们介绍了由最初的"修复"所产生的问题,以及使用Kerberos和自助密码重置的一些技巧。
截至2016年10月11日,微软重新发布了与https://technet.microsoft.com/en-us/library/security/ms16-101.aspx以解决由原始更新引起的问题(您可以在中阅读https://support.microsoft.com/en-us/kb/3177108包括您无法再更改本地帐户的密码(。