Get password from a user with UserManager

[亡魂溺海] 提交于 2019-12-08 12:25:21

问题


I'm making a website with MVC5 ASP.NET. I'm using Identity framework 2.0 implement class with properties such as passwordhash, username, email, emailconfirmed and so on. I'm using userManager.ChangePassword(user.Id, Oldpassword, Newpassword);, but i can't figure out how i should get a password from a user as plain text (string)

    [HttpPost]
    public ActionResult ChangePassword(AspNetUsersViewModel userView)
    {

        UserManager<IdentityUser> userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>());

        var result = userManager.ChangePassword(_User.Id, "123456789", userView.Password);

        return RedirectToAction("Index", "ConfigUser");

    }

As now I'm i have hardcoded users current password "123456789" to test if it works, and it does. I hope you guys can help.


回答1:


  1. Add password input to the View inside the form tag

      <input type="password" id= "userNewPassword" name="userNewPassword">
    
  2. Pass the userNewPasswor as string to the controller after the userView and the pass it to the UserManager

      [HttpPost]
      public ActionResult ChangePassword(
                AspNetUsersViewModel userView,
                string userNewPassword){
    
                           UserManager<IdentityUser> userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>());
                           var result = userManager.ChangePassword(_User.Id, userNewPassword , userView.Password);
                           return RedirectToAction("Index", "ConfigUser");
    
    
                                     }
    

Note: the Best way is to Modify the userView and add the userNewPassword to the model

Update:

in the visual studio 2013 the if you used the asp.net default template you will find the flowing class

public class ChangePasswordBindingModel
{
    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Current password")]
    public string OldPassword { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "New password")]
    public string NewPassword { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm new password")]
    [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}


来源:https://stackoverflow.com/questions/30290863/get-password-from-a-user-with-usermanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!