比较当前密码与数据库密码ASP.NET MVC与DataAnnotations

时间:2018-08-06 09:59:36

标签: asp.net-mvc

如何将用户的CurrentPassword与数据库中存储的密码进行比较?

我想让我的当前密码与数据库密码进行比较,但是我该怎么做?数据库密码已通过MD5加密。

    public async Task<IActionResult> ChangePassword(ChangePasswordVM changePasswordVM, string returnUrl = null)
    {
        LeaveUser _User = await _userManager.GetUserAsync(User);
        if (ModelState.IsValid)
        {
            var result = await _userManager.ChangePasswordAsync(_User, changePasswordVM.CurrentPassword, changePasswordVM.ConfirmNewPassword);
            if (result.Succeeded)
            {
                if (returnUrl == null)
                    return RedirectToAction("Index", "Home");
                else
                    return LocalRedirect(returnUrl);
            }
            ModelState.AddModelError("", "Invalid Password Change Attempt.");
        }
        return View();
    }

    [Required(ErrorMessage = "Please enter Current Password")]
    [DataType(DataType.Password)]
    [Display(Name = "Current Password : ")]
    public string CurrentPassword { get; set; }

    [Required(ErrorMessage = "Please enter New Password")]
    [MinLength(8), MaxLength(20)]
    [DataType(DataType.Password)]
    [Display(Name = "New Password : ")]
    public string NewPassword { get; set; }

    [Required(ErrorMessage = "Please confirm Password")]
    [MinLength(8), MaxLength(20)]
    [DataType(DataType.Password)]
    [Compare("NewPassword", ErrorMessage = "The password does not match the confirmation password.")]
    [Display(Name = "Confirm New Password : ")]
    public string ConfirmNewPassword { get; set; }

0 个答案:

没有答案