【发布时间】:2016-12-31 16:00:00
【问题描述】:
我有以下代码作为我的 Active Directory 用户的 Web 应用程序的一部分,以便能够更新他们的密码(同时用于 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:00 AM]:满足密码要求。
[8:37:00 AM]:正在连接 LDAP。
[8:37:00 AM]:LDAP 已连接,正在搜索 SAMAccountName 的目录
[8:37:01 AM]:获取用户条目。
[8:37:01 AM]:设置密码
[8:37:01 AM] : 无法为 jason 重置 Windows 密码。
调用的目标已抛出异常。
系统无法联系域控制器来处理身份验证请求。请稍后再试。 (HRESULT 异常:0x800704F1)
使用“SetPassword”的“force”选项仍然可以正常工作,但非管理员用户可以调用的“ChangePassword”方法则不行。
【问题讨论】:
标签: c# active-directory directoryservices change-password account-management