From 1e2fa384cafee70f0f72b676d73249a7b65a62a6 Mon Sep 17 00:00:00 2001 From: he <109787524@qq.com> Date: Fri, 6 Dec 2024 10:01:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=99=BB=E5=BD=95=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Service/Management/UserService.cs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/IRaCIS.Core.Application/Service/Management/UserService.cs b/IRaCIS.Core.Application/Service/Management/UserService.cs index 3091fd0..71c39dc 100644 --- a/IRaCIS.Core.Application/Service/Management/UserService.cs +++ b/IRaCIS.Core.Application/Service/Management/UserService.cs @@ -9,6 +9,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.Options; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Logging; +using static IRaCIS.Core.Domain.Share.StaticData; namespace IRaCIS.Application.Services { @@ -614,11 +615,41 @@ namespace IRaCIS.Application.Services { var userLoginReturnModel = new LoginReturnDTO(); + #region 错误验证 + // 生成缓存键 + string cacheKey = userName+"LoginError"; + + int lockoutMinutes = 30; + int maxFailures = 5; + // 从缓存中获取登录失败次数 + int? failCount = (int?)_cache.Get(cacheKey); + + if (failCount == null) + { + failCount = 0; + } + + //每次登录 都重置缓存时间 + _cache.Set(cacheKey, failCount, TimeSpan.FromMinutes(lockoutMinutes)); + + + if (failCount >= maxFailures) + { + + string error = $"The password has been entered incorrectly {maxFailures} times consecutively. Your account has been locked and you are required to wait for {lockoutMinutes} minutes before attempting to log in again."; + + //$"密码连续错误{maxFailures}次,当前账号已被限制登录,请等待 {lockoutMinutes} 分钟后再试。" + throw new BusinessValidationFailedException(error); + } + #endregion var loginUser = await _userRepository.Where(u => EF.Functions.Collate(u.UserName, "SQL_Latin1_General_CP1_CS_AS") == userName && u.Password == password).ProjectTo(_mapper.ConfigurationProvider).FirstOrDefaultAsync(); if (loginUser == null) { + + failCount++; + _cache.Set(cacheKey, failCount, TimeSpan.FromMinutes(lockoutMinutes)); //此处下面 代码 为了支持医生也能登录 而且前端不加选择到底是管理用户 还是医生用户 奇怪的需求 无法理解 var loginDoctor = await _doctorRepository.Where(u => u.Phone == userName && u.Password == password).ProjectTo(_mapper.ConfigurationProvider).FirstOrDefaultAsync();