From d194545304506d6b1e1944f0ecc2e965332bf088 Mon Sep 17 00:00:00 2001 From: milimoe Date: Tue, 17 Oct 2023 21:58:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=BF=94=E5=9B=9E=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E5=92=8C=E5=AE=9A=E6=97=B6=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Api/Utility/TFA.cs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/Api/Utility/TFA.cs b/Api/Utility/TFA.cs index 8ba8cc7..c4ff64c 100644 --- a/Api/Utility/TFA.cs +++ b/Api/Utility/TFA.cs @@ -14,18 +14,31 @@ public string GetTFACode(string username) { - return TFACodes.ContainsKey(username) ? TFACodes[username] : Verification.CreateVerifyCode(Library.Constant.VerifyCodeType.MixVerifyCode, 5); + string code = TFACodes.ContainsKey(username) ? TFACodes[username] : Verification.CreateVerifyCode(Library.Constant.VerifyCodeType.MixVerifyCode, 5); + TaskUtility.RunTimer(() => + { + // 十分钟后删除此码 + TFACodes.Remove(username, out _); + }, 1000 * 10 * 60); + return code; } - public bool Authenticate(string username, string code) + public bool Authenticate(string username, string code, out string msg) { - if (!IsAvailable(username)) return false; - if (TFACodes.ContainsKey(username) && TFACodes[username] == code) + msg = ""; + if (!IsAvailable(username)) { + msg = "此账号不需要双重认证。"; + return false; + } + if (TFACodes.ContainsKey(username) && TFACodes.TryGetValue(username, out string? checkcode) && checkcode != null && checkcode == code) + { + TFACodes.Remove(username); return true; } else { + msg = "验证码错误或已过期。"; return false; } }