Skip to content

Commit

Permalink
Fix verification code count increment logic in AccountService.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
luoyunchong committed May 19, 2024
1 parent ee9357c commit b8cd6c5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/LinCms.Application/Cms/Account/AccountContracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public class AccountContracts
/// </summary>
public static string SendPasswordResetCode_VerificationCode = "AccountService.SendPasswordResetCode.VerificationCode.{0}";

public static string SendPasswordResetCode_VerificationCode_Count = "AccountService.SendPasswordResetCode.VerificationCode.{0}.Count";

}
22 changes: 22 additions & 0 deletions src/LinCms.Application/Cms/Account/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,39 @@ private async Task<LinUser> 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
}

Expand Down

0 comments on commit b8cd6c5

Please sign in to comment.