AuthService.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using Microsoft.Extensions.Localization;
  2. using ERP.Core.Dto;
  3. using ERP.Core.Entity;
  4. using ERP.Core.Enum;
  5. using ERP.Core.Repository;
  6. using ERP.Core.Resource;
  7. using ERP.Framework.Cache;
  8. using ERP.Framework.Config;
  9. using ERP.Framework.Constants;
  10. using ERP.Framework.Utils;
  11. namespace ERP.Core.Service
  12. {
  13. public class AuthService
  14. {
  15. private readonly IStringLocalizer<AuthI18N> _localizer;
  16. private readonly SysUserRepository _sysUserRepository;
  17. public AuthService(IStringLocalizer<AuthI18N> localizer, SysUserRepository sysUserRepository)
  18. {
  19. _localizer = localizer;
  20. _sysUserRepository = sysUserRepository;
  21. }
  22. /// <summary>
  23. /// 获取登录用户
  24. /// </summary>
  25. /// <param name="securityConfig">设置</param>r
  26. /// <param name="loginName">用户名</param>
  27. /// <param name="password">密码</param>
  28. /// <param name="deviceKey">设备唯一值</param>
  29. /// <returns></returns>
  30. /// <exception cref="Exception"></exception>
  31. public LoginUser GetLoginUser(
  32. SecurityConfig securityConfig
  33. , string loginName
  34. , string password)
  35. {
  36. var user = _sysUserRepository.FirstOrDefault(t => t.UserName == loginName);
  37. if (user == null)
  38. {
  39. throw new Exception(string.Format(_localizer["User.NotExist"], loginName));
  40. }
  41. CheckUserStatus(user);
  42. CheckLogin(securityConfig, password, user);
  43. return new LoginUser();
  44. }
  45. private void CheckUserStatus(SysUser user)
  46. {
  47. if (user.UserStatus == StatusEnum.Enable)
  48. {
  49. return;
  50. }
  51. else if (user.UserStatus == StatusEnum.Disable)
  52. {
  53. throw new Exception(string.Format(_localizer["User.Bloked"], user.UserName));
  54. }
  55. }
  56. private void CheckLogin(SecurityConfig securityConfig, string password, SysUser loginUser)
  57. {
  58. var passwordErrorRedisKey = AuthConstant.PasswordError + IPUtil.GetClientIp();
  59. var errorNumber = RedisHelper.Get(passwordErrorRedisKey);
  60. var hasErrorRedis = !errorNumber.IsNullOrEmpty();
  61. if (hasErrorRedis && errorNumber.ToInt() >= securityConfig.MaxRetryCount)
  62. {
  63. // Todo 记录登录信息(超出限制期间登录)
  64. throw new Exception(string.Format(_localizer["User.Password.RetryLimitExceed"], errorNumber, securityConfig.LockTime));
  65. }
  66. var isLogin = CryptoUtil.BCValify(password, loginUser.Password);
  67. if (!isLogin)
  68. {
  69. if (!hasErrorRedis)
  70. {
  71. RedisHelper.SaveExpire(passwordErrorRedisKey, AuthConstant.FirstPasswordError, securityConfig.LockTime);
  72. }
  73. else
  74. {
  75. var number = int.Parse(errorNumber) + 1;
  76. RedisHelper.Save(passwordErrorRedisKey, number.ToString());
  77. }
  78. throw new Exception(_localizer["User.Password.Error"]);
  79. }
  80. if (hasErrorRedis)
  81. {
  82. RedisHelper.Delete(passwordErrorRedisKey);
  83. }
  84. }
  85. }
  86. }