我正在使用内部网,我刚刚在用户的个人资料中添加了一项功能来更改密码。
正如您在以下控制器中看到的那样:
[HttpPost]
public ActionResult ChangePassword(Employee objToEdit, FormCollection form, LocalPasswordModel model) // Find how to obtain "OldPassword" from AccountModel
{
objToEdit.Login = User.Identity.Name;
string name = objToEdit.FirstName;
string pwd = form["NewPassword"];
string confirm = form["ConfirmPassword"];
if (_service.Edit_password(objToEdit, pwd, confirm)) // Checks if NewPassword and ConfirmPassword are the same, and does some syntax checking
{
bool changePasswordSucceeded;
try
{
changePasswordSucceeded = WebSecurity.ResetPassword(WebSecurity.GeneratePasswordResetToken(objToEdit.Login), pwd); // Seems to work
}
catch (Exception)
{
changePasswordSucceeded = false;
}
if (changePasswordSucceeded)
{
return RedirectToAction("Index", new { Message = CRAWebSiteMVC.Controllers.AccountController.ManageMessageId.ChangePasswordSuccess });
}
else
{
ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
}
return new RedirectResult(Url.Action("Index"));
}
return View();
}
到目前为止,用户只需输入新密码和确认密码即可。我希望添加“输入您当前的密码”功能,但我无法找到检索用户当前密码的方法!
用户个人资料DB不再包含密码列,如果有任何帮助,我会使用表单身份验证。
编辑:感谢您的帮助,为了解决我的问题,我只需通过以下方式替换ResetPassword行:
changePasswordSucceeded = WebSecurity.ChangePassword(objToEdit.Login, current, pwd);
如果失败,则会直接显示当前密码错误的错误消息。
答案 0 :(得分:4)
你不能!
这实际上是一项安全功能。您绝不应以纯文本格式存储密码。
好消息是,您不需要自己进行比较:
相反,使用类似ValidateUser
的内容让成员资格提供程序验证提供的密码。在幕后,此方法将散列密码并将其与数据库中包含的散列版本进行比较。
编辑:
另请注意,由于您使用的是WebSecurity
类,因此有一种方法ChangePassword
可以接受当前密码。似乎该方法将检查当前密码是否与指定的currentPassword
参数匹配。也许你应该使用这个而不是ResetPassword