Переглянути джерело

优化密码登录、修改密码、新增用户密码加密

zhontai 2 роки тому
батько
коміт
5097558e42

+ 17 - 0
src/platform/ZhonTai.Admin/Domain/User/PasswordEncryptType.cs

@@ -0,0 +1,17 @@
+namespace ZhonTai.Admin.Domain.User;
+
+/// <summary>
+/// 密码加密类型
+/// </summary>
+public enum PasswordEncryptType
+{
+    /// <summary>
+    /// 32位MD5加密
+    /// </summary>
+    MD5Encrypt32 = 0,
+
+    /// <summary>
+    /// 标准标识密码哈希
+    /// </summary>
+    PasswordHasher = 1,
+}

+ 7 - 1
src/platform/ZhonTai.Admin/Domain/User/UserEntity.cs

@@ -29,9 +29,15 @@ public partial class UserEntity : EntityTenant
     /// <summary>
     /// 密码
     /// </summary>
-    [Column(StringLength = 60)]
+    [Column(StringLength = 200)]
     public string Password { get; set; }
 
+    /// <summary>
+    /// 密码加密类型
+    /// </summary>
+    [Column(MapType = typeof(int?))]
+    public PasswordEncryptType? PasswordEncryptType { get; set; }
+
     /// <summary>
     /// 姓名
     /// </summary>

+ 1 - 1
src/platform/ZhonTai.Admin/Services/Auth/AuthService.cs

@@ -242,7 +242,7 @@ public class AuthService : BaseService, IAuthService, IDynamicApi
             var valid = user?.Id > 0;
             if(valid)
             {
-                if (_appConfig.PasswordHasher)
+                if (user.PasswordEncryptType == PasswordEncryptType.PasswordHasher)
                 {
                     var passwordVerificationResult = _passwordHasher.VerifyHashedPassword(user, user.Password, input.Password);
                     valid = passwordVerificationResult == PasswordVerificationResult.Success || passwordVerificationResult == PasswordVerificationResult.SuccessRehashNeeded;

+ 16 - 4
src/platform/ZhonTai.Admin/Services/User/UserService.cs

@@ -323,10 +323,12 @@ public partial class UserService : BaseService, IUserService, IDynamicApi
         if (_appConfig.PasswordHasher)
         {
             entity.Password = _passwordHasher.HashPassword(entity, input.Password);
+            entity.PasswordEncryptType = PasswordEncryptType.PasswordHasher;
         }
         else
         {
             entity.Password = MD5Encrypt.Encrypt32(input.Password);
+            entity.PasswordEncryptType = PasswordEncryptType.MD5Encrypt32;
         }
         var user = await _userRepository.InsertAsync(entity);
         var userId = user.Id;
@@ -391,10 +393,18 @@ public partial class UserService : BaseService, IUserService, IDynamicApi
                 input.Password = _appConfig.DefaultPassword;
             }
 
-            input.Password = MD5Encrypt.Encrypt32(input.Password);
-
             var entity = Mapper.Map<UserEntity>(input);
             entity.Type = UserType.Member;
+            if (_appConfig.PasswordHasher)
+            {
+                entity.Password = _passwordHasher.HashPassword(entity, input.Password);
+                entity.PasswordEncryptType = PasswordEncryptType.PasswordHasher;
+            }
+            else
+            {
+                entity.Password = MD5Encrypt.Encrypt32(input.Password);
+                entity.PasswordEncryptType = PasswordEncryptType.MD5Encrypt32;
+            }
             var user = await _userRepository.InsertAsync(entity);
 
             return user.Id;
@@ -563,11 +573,13 @@ public partial class UserService : BaseService, IUserService, IDynamicApi
         }
         if (_appConfig.PasswordHasher)
         {
-            entity.Password = _passwordHasher.HashPassword(entity, input.Password);
+            entity.Password = _passwordHasher.HashPassword(entity, password);
+            entity.PasswordEncryptType = PasswordEncryptType.PasswordHasher;
         }
         else
         {
-            entity.Password = MD5Encrypt.Encrypt32(input.Password);
+            entity.Password = MD5Encrypt.Encrypt32(password);
+            entity.PasswordEncryptType = PasswordEncryptType.MD5Encrypt32;
         }
         await _userRepository.UpdateAsync(entity);
         return password;

+ 20 - 0
src/platform/ZhonTai.Admin/ZhonTai.Admin.xml

@@ -3116,6 +3116,21 @@
             数据范围
             </summary>
         </member>
+        <member name="T:ZhonTai.Admin.Domain.User.PasswordEncryptType">
+            <summary>
+            密码加密类型
+            </summary>
+        </member>
+        <member name="F:ZhonTai.Admin.Domain.User.PasswordEncryptType.MD5Encrypt32">
+            <summary>
+            32位MD5加密
+            </summary>
+        </member>
+        <member name="F:ZhonTai.Admin.Domain.User.PasswordEncryptType.PasswordHasher">
+            <summary>
+            标准标识密码哈希
+            </summary>
+        </member>
         <member name="T:ZhonTai.Admin.Domain.User.UserEntity">
             <summary>
             用户
@@ -3131,6 +3146,11 @@
             密码
             </summary>
         </member>
+        <member name="P:ZhonTai.Admin.Domain.User.UserEntity.PasswordEncryptType">
+            <summary>
+            密码加密类型
+            </summary>
+        </member>
         <member name="P:ZhonTai.Admin.Domain.User.UserEntity.Name">
             <summary>
             姓名