登录限制提交

Uat_Study
hang 2023-04-28 14:33:39 +08:00
parent 2eecca60f0
commit 5f119d6f0e
1 changed files with 37 additions and 30 deletions

View File

@ -85,7 +85,7 @@ namespace IRaCIS.Application.Services
if (oldPwd != null && oldPwd == newPwd) if (oldPwd != null && oldPwd == newPwd)
{ {
//---新密码与旧密码相同。 //---新密码与旧密码相同。
throw new BusinessValidationFailedException(_localizer["User_NewOldPwdSame"]); throw new BusinessValidationFailedException(_localizer["User_NewOldPwdSame"]);
} }
@ -94,13 +94,13 @@ namespace IRaCIS.Application.Services
if (oldPwd != null && dbUser.Password != oldPwd) if (oldPwd != null && dbUser.Password != oldPwd)
{ {
//---旧密码验证失败。 //---旧密码验证失败。
throw new BusinessValidationFailedException(_localizer["User_OldPwdInvalid"]); throw new BusinessValidationFailedException(_localizer["User_OldPwdInvalid"]);
} }
if (dbUser.Password == newPwd) if (dbUser.Password == newPwd)
{ {
//---新密码与旧密码相同。 //---新密码与旧密码相同。
throw new BusinessValidationFailedException(_localizer["User_NewOldPwdSame"]); throw new BusinessValidationFailedException(_localizer["User_NewOldPwdSame"]);
} }
@ -159,7 +159,7 @@ namespace IRaCIS.Application.Services
//检查验证码是否失效 //检查验证码是否失效
if (verificationRecord.ExpirationTime < DateTime.Now) if (verificationRecord.ExpirationTime < DateTime.Now)
{ {
//---验证码已经过期。 //---验证码已经过期。
return ResponseOutput.NotOk(_localizer["User_VerificationCodeExpired"]); return ResponseOutput.NotOk(_localizer["User_VerificationCodeExpired"]);
} }
@ -361,7 +361,7 @@ namespace IRaCIS.Application.Services
if (verificationRecord.ExpirationTime < DateTime.Now) if (verificationRecord.ExpirationTime < DateTime.Now)
{ {
//---验证码已经过期。 //---验证码已经过期。
throw new BusinessValidationFailedException(_localizer["User_VerificationCodeExpired"]); throw new BusinessValidationFailedException(_localizer["User_VerificationCodeExpired"]);
} }
else //验证码正确 并且 没有超时 else //验证码正确 并且 没有超时
@ -465,7 +465,7 @@ namespace IRaCIS.Application.Services
public async Task<PageOutput<UserListDTO>> GetUserList(UserListQueryDTO param) public async Task<PageOutput<UserListDTO>> GetUserList(UserListQueryDTO param)
{ {
var userQueryable = _userRepository.Where(x => x.UserTypeEnum != UserTypeEnum.SuperAdmin) var userQueryable = _userRepository.Where(x => x.UserTypeEnum != UserTypeEnum.SuperAdmin)
.WhereIf(!string.IsNullOrWhiteSpace(param.UserName), t => t.UserName.Contains(param.UserName) ) .WhereIf(!string.IsNullOrWhiteSpace(param.UserName), t => t.UserName.Contains(param.UserName))
.WhereIf(!string.IsNullOrWhiteSpace(param.RealName), t => t.FullName.Contains(param.RealName)) .WhereIf(!string.IsNullOrWhiteSpace(param.RealName), t => t.FullName.Contains(param.RealName))
.WhereIf(!string.IsNullOrWhiteSpace(param.Phone), t => t.Phone.Contains(param.Phone)) .WhereIf(!string.IsNullOrWhiteSpace(param.Phone), t => t.Phone.Contains(param.Phone))
.WhereIf(!string.IsNullOrWhiteSpace(param.OrganizationName), t => t.OrganizationName.Contains(param.OrganizationName)) .WhereIf(!string.IsNullOrWhiteSpace(param.OrganizationName), t => t.OrganizationName.Contains(param.OrganizationName))
@ -617,34 +617,33 @@ namespace IRaCIS.Application.Services
public async Task<IResponseOutput<LoginReturnDTO>> Login(string userName, string password) public async Task<IResponseOutput<LoginReturnDTO>> Login(string userName, string password)
{ {
//const string cachePrefix = "login-failures:"; const string cachePrefix = "login-failures:";
//const int maxFailures = 3; const int maxFailures = 3;
//const int lockoutMinutes = 3; const int lockoutMinutes = 1;
//// 生成缓存键 // 生成缓存键
//string cacheKey = $"{cachePrefix}{userName}"; string cacheKey = $"{cachePrefix}{userName}";
//// 从缓存中获取登录失败次数 // 从缓存中获取登录失败次数
//int? failCount = _cache.Get<int?>(cacheKey); int? failCount = _cache.Get<int?>(cacheKey);
//if (failCount != null) if (failCount == null)
//{ {
failCount = 0;
}
// _cache.Set(cacheKey + ":locked", true, TimeSpan.FromMinutes(lockoutMinutes)); //每次登录 都重置缓存时间
_cache.Set(cacheKey, failCount, TimeSpan.FromMinutes(lockoutMinutes));
// if (failCount >= maxFailures)
// {
// throw new BusinessValidationFailedException($"账号已被锁定,请等待 {lockoutMinutes} 分钟后再试。");
// }
//}
//if (failCount >= maxFailures) if (failCount >= maxFailures)
//{ {
// // 如果登录失败次数达到了 3 次,则锁定用户 throw new BusinessValidationFailedException($"密码连续错误3次,当前账号已被限制登录,请等待 {lockoutMinutes} 分钟后再试。");
// _cache.Set(cacheKey + ":locked", true, TimeSpan.FromMinutes(lockoutMinutes)); }
//}
@ -674,7 +673,9 @@ namespace IRaCIS.Application.Services
#endregion #endregion
//错误次数累加
failCount++;
_cache.Set(cacheKey, failCount, TimeSpan.FromMinutes(lockoutMinutes));
return ResponseOutput.NotOk(_localizer["User_CheckNameOrPw"], new LoginReturnDTO()); return ResponseOutput.NotOk(_localizer["User_CheckNameOrPw"], new LoginReturnDTO());
@ -688,6 +689,12 @@ namespace IRaCIS.Application.Services
return ResponseOutput.NotOk(_localizer["User_Disabled"], new LoginReturnDTO()); return ResponseOutput.NotOk(_localizer["User_Disabled"], new LoginReturnDTO());
} }
//登录成功 清除缓存
_cache.Set(cacheKey, 0, TimeSpan.FromMinutes(lockoutMinutes));
userLoginReturnModel.BasicInfo = loginUser; userLoginReturnModel.BasicInfo = loginUser;
// 登录 清除缓存 // 登录 清除缓存