自从 Microsoft 发布了以下两个补丁后,我的 C# 代码就停止工作,并且在尝试更改密码时总是发回一条错误消息(系统无法联系域 Controller 来为身份验证请求提供服务。请稍后重试。) .
https://support.microsoft.com/en-us/kb/3167679
https://support.microsoft.com/en-us/kb/3177108
private void ChangePassword(string username, string oldPassword, string newPassword)
{
string machineName = Environment.MachineName;
string contextUser = String.Format(@"{0}\{1}", machineName, username);
using (PrincipalContext ctx = new PrincipalContext(ContextType.Machine, machineName, null, ContextOptions.Negotiate, contextUser, oldPassword))
using (UserPrincipal user = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, username))
{
user.ChangePassword(oldPassword, newPassword);
}
}
如果提交的凭证无效,我会收到一条特定的错误消息,指出错误的用户名或密码,因此提交的凭证有效。
我尝试了很多方案,但都失败了:
- 使用我想更改 PrincipalContext 密码的用户的用户名/密码。
- 为 PrincipalContext 使用不同的用户/密码。
- 为 PrincipalContext 使用本地管理员用户/密码。
没有效果。
阅读了很多关于这种“新行为”的文章,但我似乎无法找到一种方法来更改有效的、未锁定的、未禁用的本地用户。
这些帖子与我的相似,似乎有些人找到了解决方法,但仅适用于 Active Directory 而不是本地用户帐户。
C# Active Directory Invoke “ChangePassword” cannot contact domain
Changing Active Directory user passwords in c#/asp.net after MS patch KB3167679
c# Change AD password Directoryservices
谁能告诉我我的代码有什么问题,并解释一下为什么打补丁后代码会出错?
最佳答案
如果您只想更改本地用户的密码,您应该可以使用 NetUserChangePassword .
这对我仍然有效。至少对于本地机器(使用它的名称作为域名参数)。
关于本地帐户的 C# UserPrincipal.ChangePassword 在 KB3167679 和 KB3177108 之后被破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39349566/