Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net membership change password without knowing old one

Evaluting the method signature, it is required to know old password while changing it.

membershipUser.ChangePassword(userWrapper.OldPassword, userWrapper.Password)

Is there any way to change password without knowing old one.

like image 837
Lalit Avatar asked Feb 16 '11 08:02

Lalit


People also ask

How can I reset my asp net membership password?

MembershipUser usr = Membership. GetUser(username); string resetPwd = usr. ResetPassword(); usr. ChangePassword(resetPwd, newPassword);

How to Reset password c#?

Local users who forget their password can have a security token sent to their email account, enabling them to reset their password. The user will soon get an email with a link allowing them to reset their password. Selecting the link will take them to the Reset page.


2 Answers

 string username = "username";
 string password = "newpassword";
 MembershipUser mu = Membership.GetUser(username);
 mu.ChangePassword(mu.ResetPassword(), password);
like image 94
ajma Avatar answered Oct 27 '22 18:10

ajma


The other answers here are correct, but can leave the password in an unknown state.

ChangePassword will throw exceptions if the password doesn't meet the requirements laid out in Web.Config (minimum length, etc.). But it only fails after ResetPassword has been called, so the password will not be known to the original user or to the person who's tried to change it. Check for complexity requirements before changing the password to avoid this:

var user = Membership.GetUser(userName, false);

if ((newPassword.Length >= Membership.MinRequiredPasswordLength) &&
    (newPassword.ToCharArray().Count(c => !Char.IsLetterOrDigit(c)) >=
         Membership.MinRequiredNonAlphanumericCharacters) &&
    ((Membership.PasswordStrengthRegularExpression.Length == 0) ||
         Regex.IsMatch(newPassword, Membership.PasswordStrengthRegularExpression))) {

    user.ChangePassword(user.ResetPassword(), newPassword);
} else {
    // Tell user new password isn't strong enough
}
like image 24
Rob Church Avatar answered Oct 27 '22 16:10

Rob Church