From 6f1a061dad6982923fd414d14db670fb7815b4bd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=8E=8B=E7=82=9C=E7=BF=94?= <2307953404@qq.com>
Date: Thu, 31 Oct 2024 18:29:35 +0800
Subject: [PATCH] 1
---
asg_form/Controllers/login.cs | 21 +++++++++++++++------
asg_form/Controllers/prerfunctions/Draw.cs | 11 +++++------
2 files changed, 20 insertions(+), 12 deletions(-)
diff --git a/asg_form/Controllers/login.cs b/asg_form/Controllers/login.cs
index 2941f83..3711f13 100644
--- a/asg_form/Controllers/login.cs
+++ b/asg_form/Controllers/login.cs
@@ -218,6 +218,8 @@ namespace asg_form.Controllers
}
public readonly CaptchaService _captchaService = new CaptchaService();
+ public string captchaNow = "7777";
+
///
/// 验证码生成
///
@@ -228,11 +230,17 @@ namespace asg_form.Controllers
var captchaService = new CaptchaService();
var (captchaImage, captchaCode) = captchaService.GenerateCaptcha();
- // 这里可以将验证码存储在内存缓存或数据库中,以便后续验证
- //StoreCaptcha(captchaCode); // 自定义存储逻辑
+ captchaNow=captchaCode;
return Ok(new { captchaImage, captchaCode });
}
+
+ // 验证用户输入的验证码
+ public bool ValidateCaptcha(string userInput)
+ {
+ return userInput.Equals(captchaNow, StringComparison.OrdinalIgnoreCase);
+ }
+
///
/// 验证码注册
///
@@ -244,13 +252,14 @@ namespace asg_form.Controllers
try
{
- //验证验证码的逻辑
- //bool isCaptchaValid = ValidateCaptcha(newuser.captcha); // 自定义验证逻辑
- if (true)
+ bool isCaptchaValid = ValidateCaptcha(newuser.captcha);
+ if (captchaNow == "7777") return BadRequest(new { code = 400, message = "服务器正忙" });
+ if (!isCaptchaValid)
{
+ captchaNow = "7777";
return BadRequest(new { code = 400, message = "验证码无效" });
}
-
+ captchaNow = "7777";
var maxId = await userManager.Users.MaxAsync(u => u.Id);
User? user = await userManager.FindByNameAsync(newuser.userName);
if (user == null)
diff --git a/asg_form/Controllers/prerfunctions/Draw.cs b/asg_form/Controllers/prerfunctions/Draw.cs
index fc05195..be14b8e 100644
--- a/asg_form/Controllers/prerfunctions/Draw.cs
+++ b/asg_form/Controllers/prerfunctions/Draw.cs
@@ -31,18 +31,17 @@ public class CaptchaService
}
}
- // 验证用户输入的验证码
- public bool ValidateCaptcha(string userInput)
- {
- return userInput.Equals(generatedCaptchaCode, StringComparison.OrdinalIgnoreCase);
- }
+
// 生成随机字符
private string GenerateRandomCode(int length)
{
- const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+ const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345689";
Random random = new Random();
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
+
+
+
}