如何使用自定义 IPasswordHasher? [英] How use custom IPasswordHasher?

查看:20
本文介绍了如何使用自定义 IPasswordHasher?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了 IPasswordHasher

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;
    }
}

我在 IdentityConfig 中写入

I write in IdentityConfig

manager.PasswordHasher = new MyPasswordHasher();

但是 var user = await UserManager.FindAsync(model.Email, model.Password); 在 AccountController/Login 中不要使用 MyPasswordHaser.

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

如何在 Identity 2.1 中使用它?

How can I use it in Identity 2.1?

推荐答案

你必须把它插入到 UserManager 中:

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();
    }
}

这篇关于如何使用自定义 IPasswordHasher?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆