gpt4 book ai didi

c# - 如何使用自定义 IPasswordHasher?

转载 作者:太空狗 更新时间:2023-10-29 20:30:32 28 4
gpt4 key购买 nike

我实现了 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

manager.PasswordHasher = new MyPasswordHasher();

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

如何在 Identity 2.1 中使用它?

最佳答案

您必须将其插入到 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();
}
}

关于c# - 如何使用自定义 IPasswordHasher?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26505446/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com