首页 > 解决方案 > 将当前密码与数据库密码 ASP.NET MVC 与 DataAnnotations 进行比较

问题描述

如何将用户的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; }

标签: asp.net-mvc

解决方案


推荐阅读