gpt4 book ai didi

C# Active Directory 调用 "ChangePassword"无法联系域

转载 作者:行者123 更新时间:2023-11-30 16:47:43 25 4
gpt4 key购买 nike

我将以下代码作为我的 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] : Password Requirements Met.

[8:37:00 AM] : Connecting LDAP.

[8:37:00 AM] : LDAP Connected, searching directory for SAMAccountName

[8:37:01 AM] : Getting User Entry.

[8:37:01 AM] : Setting Password

[8:37:01 AM] : Failed to reset Windows Password for jason.


Exception has been thrown by the target of an invocation.


The system cannot contact a domain controller to service the authentication request. Please try again later. (Exception from HRESULT: 0x800704F1)

使用“SetPassword”的“force”选项仍然可以正常工作,但非管理员用户可以调用的“ChangePassword”方法则不能。

最佳答案

Change userPrincipal.ChangePassword("旧密码", "新密码");到 userPrincipal.SetPassword(model.NewPassword);

关于C# Active Directory 调用 "ChangePassword"无法联系域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39124626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com