From b8cd6c5182d4e33874afb20da56c41033d367dd0 Mon Sep 17 00:00:00 2001 From: IGeekFan Date: Sun, 19 May 2024 22:58:07 +0800 Subject: [PATCH] Fix verification code count increment logic in AccountService.cs --- .../Cms/Account/AccountContracts.cs | 2 ++ .../Cms/Account/AccountService.cs | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/LinCms.Application/Cms/Account/AccountContracts.cs b/src/LinCms.Application/Cms/Account/AccountContracts.cs index c7eff31c..9e69e9c7 100644 --- a/src/LinCms.Application/Cms/Account/AccountContracts.cs +++ b/src/LinCms.Application/Cms/Account/AccountContracts.cs @@ -16,4 +16,6 @@ public class AccountContracts /// public static string SendPasswordResetCode_VerificationCode = "AccountService.SendPasswordResetCode.VerificationCode.{0}"; + public static string SendPasswordResetCode_VerificationCode_Count = "AccountService.SendPasswordResetCode.VerificationCode.{0}.Count"; + } \ No newline at end of file diff --git a/src/LinCms.Application/Cms/Account/AccountService.cs b/src/LinCms.Application/Cms/Account/AccountService.cs index 8577eb56..8aadb099 100644 --- a/src/LinCms.Application/Cms/Account/AccountService.cs +++ b/src/LinCms.Application/Cms/Account/AccountService.cs @@ -165,17 +165,39 @@ private async Task GetUserByChecking(string inputEmailAddress) return user; } + + private async Task IncreateVerificationCodeCount(string email) + { + string keyCount = string.Format(AccountContracts.SendPasswordResetCode_VerificationCode_Count, email); + string count = await redisClient.GetAsync(keyCount); + if(count.IsNullOrWhiteSpace()) + { + await redisClient.SetAsync(keyCount, 1, 30 * 60); + } + else + { + int.TryParse(count, out int countInt); + if(countInt >= 5) + { + throw new LinCmsException("验证码已过期"); + } + await redisClient.IncrByAsync(keyCount, 1); + } + } public async Task ResetPasswordAsync(ResetEmailPasswordDto resetPassword) { string key = string.Format(AccountContracts.SendPasswordResetCode_VerificationCode, resetPassword.Email); string resetCode = await redisClient.GetAsync(key); + if (resetCode.IsNullOrWhiteSpace()) { + await IncreateVerificationCodeCount(resetPassword.Email); throw new LinCmsException("验证码已过期"); } if (resetPassword.ResetCode != resetCode) { + await IncreateVerificationCodeCount(resetPassword.Email); throw new LinCmsException("验证码不正确");//InvalidEmailConfirmationCode }