C# 哈希密码失败

C# 哈希密码失败,c#,asp.net-mvc,asp.net-core,C#,Asp.net Mvc,Asp.net Core,我正在尝试向我的asp.net核心网站添加一些基本身份验证。 我将用户存储在sqlite数据库中,我试图验证用户输入的密码,但由于某些原因,即使输入的密码正确,它也总是失败 有什么建议吗 这是我的登录操作: [HttpPost] [ValidateAntiForgeryToken] public async Task<ActionResult> Login(LoginViewModel ivm) { if (ModelState.IsValid) { var

我正在尝试向我的asp.net核心网站添加一些基本身份验证。
我将用户存储在sqlite数据库中,我试图验证用户输入的密码,但由于某些原因,即使输入的密码正确,它也总是失败

有什么建议吗

这是我的登录操作:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel ivm)
{
   if (ModelState.IsValid)
   {
      var user = _userRepo.Get(ivm.Email);
      if (user == null)
      {
         ModelState.AddModelError("UserError", "User not found");
         return View("Index", ivm);
      }
      PasswordHasher<User> hasher = new PasswordHasher<User>();
      var result = hasher.VerifyHashedPassword(user, user.Password, ivm.Password);
      if (result != PasswordVerificationResult.Failed)
      {
         string role = "";
         if (user.Role == Models.Enums.Role.Admin)
            role = "Admin";
         else
            role = "User";
         var claims = new[] { new Claim(ClaimTypes.Name, user.Id.ToString()), new Claim(ClaimTypes.Role, role) };
         var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
         await AuthenticationHttpContextExtensions.SignInAsync(HttpContext, CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));
         return RedirectToAction("Index", "Home");
      }
      else
      {
         ModelState.AddModelError("PasswordError", "Wrong password");
         return View("Index", ivm);
      }
   }
   else
   {
      ModelState.AddModelError("ModelError", "ModelError");
      return View("Index", ivm);
   }
}
当前初始化仅为管理员用户:

            var user = new User
            {
                Email = "email.com",
                Role = Models.Enums.Role.Admin,
                Id = Guid.NewGuid()
            };
            PasswordHasher<User> phw = new PasswordHasher<User>();
            string hashed = phw.HashPassword(user, "superpassword");
            user.Password = hashed;
            db.Users.Add(user);
            db.SaveChanges();
var user=新用户
{
Email=“Email.com”,
Role=Models.Enums.Role.Admin,
Id=Guid.NewGuid()
};
PasswordHasher phw=新的PasswordHasher();
字符串hashed=phw.HashPassword(用户,“超级密码”);
user.Password=散列;
db.Users.Add(用户);
db.SaveChanges();

在我的ASP.NET核心项目中,我使用了
UserManager
,它可以很好地处理密码检查

bool correctPassword = await _userManager.CheckPasswordAsync(user, password);
UserManager
还处理用户创建,而无需处理密码哈希器

重载之一:

public virtual Task<IdentityResult> CreateAsync(TUser user, string password);
下面是相关的位:
PasswordHash=newpasswordhasher



您的用户类是什么样子的,特别是密码属性?此外,如何添加新用户?注册期间,哪个函数在执行哈希?@mcbowes看到updatei m srry,因为我是新加入core的,但这需要什么样的设置?我正在假设一些数据库连接?嗨@Pio,我刚刚更新了我的答案-请参阅我关于
Password
&
PasswordHash
用户
类成员的注释好的,是的,你的用户类与我的类没有任何不同。但是我不明白这个用户管理器是从哪里来的,我猜它需要被注入,但是我应该在哪里设置它呢?像数据库连接,它应该使用哪个用户类,等等?现在我有了一个地方,我有一个'真的'现在为我的用户,谢谢你的指导
public virtual Task<IdentityResult> CreateAsync(TUser user, string password);
hostAdminUser = new ApplicationUser()
{
    UserName = SetupConsts.Users.Host.UserName,
    Email = SetupConsts.Users.Host.Email,
    EmailConfirmed = true,
    PasswordHash = new PasswordHasher<ApplicationUser>().HashPassword(hostAdminUser, SetupConsts.Users.Passwords.Default)
};

await _userManager.CreateAsync(hostAdminUser);
public class AuthController : Controller
{
    private UserManager<ApplicationUser> _userManager;

    public AuthController(
        UserManager<ApplicationUser> userManager
        )
    {
        _userManager = userManager;
    }
services.AddTransient<UserManager<ApplicationUser>>();