How use custom IPasswordHasher?

2020-04-05 08:29发布

I implements IPasswordHasher

public class MyPasswordHasher : IPasswordHasher
{
    public string HashPassword(string password)
    {
        using (SHA256 mySHA256 = SHA256Managed.Create())
        {
            byte[] hash = mySHA256.ComputeHash(Encoding.UTF8.GetBytes(password.ToString()));

            StringBuilder hashSB = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                hashSB.Append(hash[i].ToString("x2"));
            }
            return hashSB.ToString();
        }
    }


    public PasswordVerificationResult VerifyHashedPassword(
      string hashedPassword, string providedPassword)
    {
        if (hashedPassword == HashPassword(providedPassword))
            return PasswordVerificationResult.Success;
        else
            return PasswordVerificationResult.Failed;
    }
}

I write in IdentityConfig

manager.PasswordHasher = new MyPasswordHasher();

but var user = await UserManager.FindAsync(model.Email, model.Password); in AccountController/Login do not use MyPasswordHaser.

How can I use it in identity 2.1?

My project https://yadi.sk/d/NUL56SMccCtqd

2条回答
狗以群分
2楼-- · 2020-04-05 08:47

Use:

PasswordHasher = new MyPasswordHasher(); 

in UserManager ctor!

查看更多
甜甜的少女心
3楼-- · 2020-04-05 08:52

You have to plug it into the UserManager:

public class AppUserManager : UserManager<AppUser, int>
{
    public AppUserManager(AppUserStore a_store)
        : base(a_store)
    {
        _container = a_container;
        _emailService = _container.GetInstance<IEmailService>();

        PasswordHasher = new AppPasswordHasher();
    }
}
查看更多
登录 后发表回答