[英]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.
希望你们能帮上忙。
Add password input to the View inside the form tag 在表单标签内的视图中添加密码输入
<input type="password" id= "userNewPassword" name="userNewPassword">
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.