【发布时间】:2023-04-11 07:37:01
【问题描述】:
我正在尝试更改系统中所有成员的 AD 密码,但我的代码仅成功更改了某些成员的密码。对于不能修改密码的会员,提示如下错误:
System.NullReferenceException:对象引用未设置为对象的实例。在 C:\Users\Intern\source\repos\changep1\changep1\changep2.aspx.cs:line 52 中的 changep1.changep2.changeUserPassword(String _userID, String _oldPassword, String _newPassword) 处
这是我的 C# 代码:
public string changeUserPassword(string _userID, string _oldPassword, string _newPassword)
{
string message="";
try
{
PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, "extra.sales-comm.local", "DC=sales-comm,DC=local",
ContextOptions.SimpleBind, @"admin", "Passw@rd");
UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, _userID);
oUserPrincipal.ChangePassword(_oldPassword, _newPassword);
oUserPrincipal.Save();
}
catch (Exception e)
{
message = e.ToString();
}
return message;
}
我不明白为什么我的代码不会更改所有 AD 成员的密码。请帮忙谢谢。
【问题讨论】:
标签: c# active-directory ldap change-password principalcontext