I'm trying to change the AD password of all members in my system, but my code only changes the password for some members successfully. For members whose password can't be changed, it shows the following error:
System.NullReferenceException: Object reference not set to an instance of an object. at changep1.changep2.changeUserPassword(String _userID, String _oldPassword, String _newPassword) in C:\Users\Intern\source\repos\changep1\changep1\changep2.aspx.cs:line 52
Here is my c# code:
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;
}
I don't understand why my code doesn't change passwords for all AD members. Please help thanks.