1
This commit is contained in:
parent
b1fe6994f1
commit
6f1a061dad
@ -218,6 +218,8 @@ namespace asg_form.Controllers
|
||||
}
|
||||
public readonly CaptchaService _captchaService = new CaptchaService();
|
||||
|
||||
public string captchaNow = "7777";
|
||||
|
||||
/// <summary>
|
||||
/// 验证码生成
|
||||
/// </summary>
|
||||
@ -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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证码注册
|
||||
/// </summary>
|
||||
@ -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)
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user