繁体   English   中英

ASP.NET Identity - 自定义散列

[英]ASP.NET Identity - custom hashing

我正在尝试对从通用到 BCrypt 的 ASP.NET Identity 使用自定义散列算法。

有没有办法我可以做到这一点?

我相信以下代码应该适合您:

启动.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 的类,然后替换 Startup.cs 中的默认 IPasswordHasher

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM