简体   繁体   English

使用UserManager从用户获取密码

[英]Get password from a user with UserManager

I'm making a website with MVC5 ASP.NET. 我正在使用MVC5 ASP.NET创建一个网站。 I'm using Identity framework 2.0 implement class with properties such as passwordhash, username, email, emailconfirmed and so on. 我正在使用具有诸如passwordhash,用户名,电子邮件,emailconfirmed等属性的Identity Framework 2.0实现类。 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) 我正在使用userManager.ChangePassword(user.Id,Oldpassword,Newpassword);,但我不知道如何从用户那里获得纯文本(字符串)的密码

    [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. 到现在为止,我已经对用户的当前密码“ 123456789”进行了硬编码,以测试它是否可以正常工作。 I hope you guys can help. 希望你们能帮上忙。

  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 在userView之后将userNewPasswor作为字符串传递给控制器​​,并将其传递给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 注意:最好的方法是修改userView并将userNewPassword添加到模型中

Update: 更新:

in the visual studio 2013 the if you used the asp.net default template you will find the flowing class 在Visual Studio 2013中,如果您使用asp.net默认模板,则会发现正在流动的类

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从IdentityUser继承我在UserManager上收到错误<User> - Inherit from IdentityUser I get an error on UserManager<User> 从Hangfire Job获取UserManager - Get UserManager from Hangfire Job 正在使用的错误案例列表_userManager.CreateAsync(user,password) - List of error cases in use _userManager.CreateAsync(user, password) UserManager.CreateAsync(用户,密码)陷入无限循环 - UserManager.CreateAsync(user, password) stuck in infinite loop 测试 usermanager 创建用户但密码 hash 属性为 null - Testing usermanager creates a user but password hash property is null 从ConnectionStringSettings获取用户名和密码 - Get user and password from ConnectionStringSettings 无法从apicontroller中的OwinContext获取UserManager - Can't get UserManager from OwinContext in apicontroller 直接从 DB vs UserManager 获取 AspNet 用户是不好的做法吗? - Is it bad practice to Fetch AspNet User directly from DB vs UserManager? 无法访问已处置的 object。\r\n对象名称: 'UserManager`1 - 在调用 CreateAsync(user, model.password) 时 - Cannot access a disposed object.\r\nObject name: 'UserManager`1 - while calling CreateAsync(user, model.password) Base-64 char数组或字符串的长度无效。 在UserManager.CheckPasswordAsync(用户,密码)中 - Invalid length for a Base-64 char array or string. in UserManager.CheckPasswordAsync(user, password)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM