使用UserManager从用户获取密码 [英] Get password from a user with UserManager

查看:278
本文介绍了使用UserManager从用户获取密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVC5 ASP.NET创建一个网站. 我正在使用Identity Framework 2.0实现类,其类具有诸如passwordhash,用户名,电子邮件,emailconfirmed等属性.我正在使用userManager.ChangePassword(user.Id,Oldpassword,Newpassword);,但我不知道如何从用户那里获得纯文本(字符串)的密码

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");

    }

现在,我已经对用户的当前密码"123456789"进行了硬编码,以测试它是否可以正常工作. 希望你们能帮上忙.

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">

  • 将userNewPasswor作为字符串传递给userView之后的控制器,并将其传递给UserManager

  • 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");
    
    
                                     }
    

  • 注意:最好的方法是修改userView并将userNewPassword添加到模型中

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

    更新:

    在Visual Studio 2013中,如果您使用了asp.net默认模板,则会找到流动的类

    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; }
    }
    

    这篇关于使用UserManager从用户获取密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆