Identity Change Password in several ways

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.