If you want to change a membership user’s password with the MembershipUser.ChangePassword method you need to know the user’s old password.
The following extension method defines a SetNewPassword method that enables you to reset the password without knowing the old one:
public static class MembershipUserExtension
{
///
/// Set a new password for the membership user
///
/// Membership user to extend
/// New password
/// True if the update was successful; otherwise, false.
public static bool SetNewPassword(this MembershipUser membershipUser, string newPassword)
{
var generatedPassword = membershipUser.ResetPassword();
return membershipUser.ChangePassword(generatedPassword, newPassword);
}
}
Please note that this method only works if the Membership Properties are defined as follows:
- RequiresQuestionAndAnswer is set to false.
- and EnablePasswordReset is set to true.