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 readonly CaptchaService _captchaService = new CaptchaService();
|
||||||
|
|
||||||
|
public string captchaNow = "7777";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 验证码生成
|
/// 验证码生成
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -228,11 +230,17 @@ namespace asg_form.Controllers
|
|||||||
var captchaService = new CaptchaService();
|
var captchaService = new CaptchaService();
|
||||||
var (captchaImage, captchaCode) = captchaService.GenerateCaptcha();
|
var (captchaImage, captchaCode) = captchaService.GenerateCaptcha();
|
||||||
|
|
||||||
// 这里可以将验证码存储在内存缓存或数据库中,以便后续验证
|
captchaNow=captchaCode;
|
||||||
//StoreCaptcha(captchaCode); // 自定义存储逻辑
|
|
||||||
|
|
||||||
return Ok(new { captchaImage, captchaCode });
|
return Ok(new { captchaImage, captchaCode });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 验证用户输入的验证码
|
||||||
|
public bool ValidateCaptcha(string userInput)
|
||||||
|
{
|
||||||
|
return userInput.Equals(captchaNow, StringComparison.OrdinalIgnoreCase);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 验证码注册
|
/// 验证码注册
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -244,13 +252,14 @@ namespace asg_form.Controllers
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
//验证验证码的逻辑
|
bool isCaptchaValid = ValidateCaptcha(newuser.captcha);
|
||||||
//bool isCaptchaValid = ValidateCaptcha(newuser.captcha); // 自定义验证逻辑
|
if (captchaNow == "7777") return BadRequest(new { code = 400, message = "服务器正忙" });
|
||||||
if (true)
|
if (!isCaptchaValid)
|
||||||
{
|
{
|
||||||
|
captchaNow = "7777";
|
||||||
return BadRequest(new { code = 400, message = "验证码无效" });
|
return BadRequest(new { code = 400, message = "验证码无效" });
|
||||||
}
|
}
|
||||||
|
captchaNow = "7777";
|
||||||
var maxId = await userManager.Users.MaxAsync(u => u.Id);
|
var maxId = await userManager.Users.MaxAsync(u => u.Id);
|
||||||
User? user = await userManager.FindByNameAsync(newuser.userName);
|
User? user = await userManager.FindByNameAsync(newuser.userName);
|
||||||
if (user == null)
|
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)
|
private string GenerateRandomCode(int length)
|
||||||
{
|
{
|
||||||
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345689";
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
return new string(Enumerable.Repeat(chars, length)
|
return new string(Enumerable.Repeat(chars, length)
|
||||||
.Select(s => s[random.Next(s.Length)]).ToArray());
|
.Select(s => s[random.Next(s.Length)]).ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user