authentication - 在.NET Core 2.0中实现bcrypt

标签 authentication hash .net-core bcrypt

我是使用 Core 进行开发的新手。我在 Visual Studio 中创建了一个 ASP.NET Core Web 应用程序 (MVC),并将个人用户帐户存储在应用程序中。我在 SQL Server 中为应用程序创建了一个数据库,更新了连接字符串,并在 NuGet 控制台中运行 Update-Database。我想覆盖密码哈希函数并使用 bcrypt 进行哈希。我希望使用 BCrypt-Core、BCrypt.Net - Next 或 Bcrypt-Official 软件包。但我不知道从哪里开始,以确保在生成密码和用户登录时覆盖散列。我的猜测是我需要覆盖PasswordHasher,但是我需要覆盖哪些方法以及当用户想要登录?任何当前实现的建议/建议/链接将不胜感激!

最佳答案

创建一个名为 BCryptPasswordHasher.cs 的类

 public class BCryptPasswordHasher<TUser> : PasswordHasher<TUser> where TUser : class
    {
        /// <summary>
        ///  Overrides instance of Microsoft.AspNetCore.Identity.PasswordHasher
        /// </summary>
        /// <param name="optionsAccessor"></param>
        public BCryptPasswordHasher(IOptions<PasswordHasherOptions> optionsAccessor = null)
        {

        }

        /// <summary>
        ///  Returns a hashed representation of the supplied password for the specified user.
        /// </summary>
        /// <param name="user"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public override string HashPassword(TUser user, string password)
        {
            return BCrypt.Net.BCrypt.HashPassword(password);
        }

        /// <summary>
        /// Returns a Microsoft.AspNetCore.Identity.PasswordVerificationResult indicating
        //     the result of a password hash comparison.
        /// </summary>
        /// <param name="user"></param>
        /// <param name="hashedPassword">The hash value for a user's stored password.</param>
        /// <param name="providedPassword"> The password supplied for comparison.</param>
        /// <returns></returns>
        public override PasswordVerificationResult VerifyHashedPassword(TUser user, string hashedPassword, string providedPassword)
        {
            if (hashedPassword == null) { throw new ArgumentNullException(nameof(hashedPassword)); }
            if (providedPassword == null) { throw new ArgumentNullException(nameof(providedPassword)); }            

            if (BCrypt.Net.BCrypt.Verify(providedPassword, hashedPassword))
            {
                return PasswordVerificationResult.Success;
            }
            else
            {
                return PasswordVerificationResult.Failed;
            }
        }    
    }

在 Startup.cs 中 - 在 AddIdentity 添加之前

 services.AddScoped<IPasswordHasher<ApplicationUser>, BCryptPasswordHasher<ApplicationUser>>();

感谢 Andrew Lock 让我完成了 90% 的任务。 https://andrewlock.net/migrating-passwords-in-asp-net-core-identity-with-a-custom-passwordhasher/

关于authentication - 在.NET Core 2.0中实现bcrypt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48591887/

相关文章:

c# - C# 3.0 中 Windows 本地用户帐户的身份验证

python - 如何将数组转换为字符,以便我可以从c中的函数返回该字符值。然后,在Python中调用它。

python - 在 Python 中使用对象 ID 作为对象的哈希值

.net-core - 方法未找到 : 'System.IServiceProvider Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider'

PHP:SSL HTTP 身份验证?

java - Play 框架 - 具有 session 身份验证的代理请求

asp.net-mvc - OWIN认证中间件 : logging off

PHP:将哈希密码与用户输入相匹配不起作用

c# - 创建目录后.NET Core控制台应用程序出现UnauthorizedAccessException

c# - 从 F# 调用 C# 异步方法会导致死锁