Good day,
We have a SharePoint site that has a customized web part for changing a user password. The accounts are from Active Directory. The web part is added to a page and allows user to change their password. But every time a user change their password, their session expires and prompts a login. Users are unable to see the Success message unless they log in again using the new password. What is the reason behind this? Is this a normal behavior?
This is the code I used to change the AD user password:
PrincipalContext _context = null;
//Get context en user object
_context = new PrincipalContext(ContextType.Domain,
Domain,
SearchString,
AdminUser,
AdminPass
);
UserPrincipal user = UserPrincipal.FindByIdentity(_context, username);
//User does not exist
if (user == null)
{
lblError.Text = "Username and/or password incorrect. Password was not changed.";
return;
}
//Check if password is expired
bool isOldPassValid = false;
DateTime? PasswordExpDate;
if (user.LastPasswordSet != null)
PasswordExpDate = ((DateTime)user.LastPasswordSet).AddDays(int.Parse(PasswordExpiresInDays));
else
PasswordExpDate = new DateTime(1970, 01, 01);
if ((user.LastPasswordSet == null || PasswordExpDate < DateTime.UtcNow) && !user.PasswordNeverExpires)
{
//Temporarly unexpire password and check credentials
user.RefreshExpiredPassword();
isOldPassValid = _context.ValidateCredentials(user.SamAccountName, oldPass);
}
else
isOldPassValid = _context.ValidateCredentials(user.SamAccountName, oldPass);
//Old password not correct
if (!isOldPassValid)
{
lblError.Text = "Username and/or old password incorrect";
return;
}
//Everything OK, change pass
user.SetPassword(newPass1);
user.Save();
lblSuccess.Text = "Your password has been changed successfully. Please close your browser and log in using your new password.";