ASP.. NET身份-自定义哈希



我试图使用自定义哈希算法的ASP。. NET标识从通用标识到BCrypt。

有办法吗?

我相信下面的代码应该为您工作:

Startup.CS:

public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddTransient<IPasswordHasher<ApplicationUser>, BCryptPasswordHasher>();
}

BCryptPasswordHasher.cs:

public class BCryptPasswordHasher : IPasswordHasher<ApplicationUser>
{
public string HashPassword(ApplicationUser user, string password)
{
return BCrypt.Net.BCrypt.HashPassword(SaltPassword(user, password), 10);
}
public PasswordVerificationResult VerifyHashedPassword(ApplicationUser user, string hashedPassword, string providedPassword)
{
if (BCrypt.Net.BCrypt.Verify(SaltPassword(user, providedPassword), hashedPassword))
return PasswordVerificationResult.Success;
return PasswordVerificationResult.Failed;
}
private string SaltPassword(ApplicationUser user, string password)
{
//TODO: salt password
}
}

你正在创建一个类实现IPasswordHasher,然后替换默认的IPasswordHasher在Startup.cs

最新更新