我被告知要让管理员在不知道其他用户原始密码的情况下更改其他用户的密码。我编写了一个代码,可以更改密码并将其成功保存在数据库中,但是当我尝试以该用户身份登录时,我无法登录。
用户控制器:
public ActionResult ChangePassword()
{
return View();
}
[HttpPost]
public ActionResult ChangePassword(int id, ViewModels.ChangePasswordViewModel model)
{
if (!SessionControlService.CheckIsLoginStillTrue(_loginsService, HttpContext))
return RedirectToAction("Login", "Account");
if (!User.IsInAnyRoles("Admin", "PropertyManager"))
return RedirectToAction("Error", "Errors",
new { error = Facility.Web.Resources.Resources.ErrorNotHavePermission });
var user = _userService.GetUser(id);
if (user == null)
return RedirectToAction("Error", "Errors",
new { error = Facility.Web.Resources.Resources.ErrorURLNotExist });
user.Password = model.NewPassword;
_userService.UpdateUser(user);
return RedirectToAction("Details", new { id = id });
}
为什么我不能使用保存在数据库中的更改密码登录?我怎样才能做到这一点?
在 ASP.NET MVC5 中,密码是散列的...您不能像这样保存明文密码。
您需要使用以下两种方法:
var manager = new ApplicationUserManager(...);
var token = manager.GeneratePasswordResetToken(userId)
manager.ResetPassword(userId, token, newPassword)
您也可以尝试ApplicationUserManager.UpdatePassword(...)
,或RemovePassword(...)
并AddPassword(...)
ApplicationUserManager
通常处于IdentityConfig.cs