【问题标题】:Implementing bcrypt in .NET Core 2.0在 .NET Core 2.0 中实现 bcrypt
【发布时间】:2018-02-02 22:37:45
【问题描述】:

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

【问题讨论】:

    标签: authentication hash .net-core bcrypt


    【解决方案1】:

    创建一个名为 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/

    【讨论】:

      猜你喜欢
      • 2018-05-28
      • 2010-10-26
      • 2021-05-15
      • 1970-01-01
      • 2018-03-13
      • 2018-07-17
      • 2018-03-21
      • 2018-04-21
      • 1970-01-01
      相关资源
      最近更新 更多