1, first use Usermanager.removepassword (UserID), clear the password, and then use Usermnager.addpassword (Userid,newpassword) to add a new password.
[HttpPost]
[Validateantiforgerytoken]
Public ActionResult ChangePassword (Changepasswordviewmodel Userviewmodel)
{
var userId = User.Identity.GetUserId ();
var _user = Usermanager.findbyid (userId);
if (modelstate.isvalid)
{
if (Usermanager.checkpassword (_user, Userviewmodel.oldpassword))//usermanager.checkpasswrod Check that the password is correct.
{
Usermanager.removepassword (userId); Empty the old password.
Usermanager.addpassword (UserId, Userviewmodel.newpassword); Add a new password.
Return redirecttoaction ("Index");
}
Else
{
Modelstate.addmodelerror ("", "the old password entered is incorrect");
Return View (Userviewmodel);
}
}
Else
{
Modelstate.addmodelerror ("", "Bind failed");
Return View (Userviewmodel);
}
}
2. Use Usermanager.gerneratepasswordresettokeno to create a GUID value for the user to reset the password, and then call ResetPassword (Userid,guid,newpassword) Value to re-create the password;
[HttpPost]
[Validateantiforgerytoken]
Public ActionResult Changeuserpassword (Resetpasswordviewmodel _resetpasswordviewmodel)
{
if (! Modelstate.isvalid)
{
Return View (_resetpasswordviewmodel);
}
var _user = Usermanager.findbyname (_resetpasswordviewmodel.username);
if (_user = = null)
return new Httpstatuscoderesult (httpstatuscode.badrequest);
Var code =usermanager.generatepasswordresettoken (_user. ID); //Create a GUID value for the user to reset the password
var result =usermanager.resetpassword (_user. Id,code,_resetpasswordviewmodel.password); //re-create password;
if (result. SUCCEEDED)
{
Return redirecttoaction ("Index");
}
return View ();
}
Identity Change Password in several ways