This commit is contained in:
王炜翔 2024-10-31 15:45:28 +08:00
parent e4dd63f2c1
commit 8b8a0010db
2 changed files with 32 additions and 24 deletions

View File

@ -17,6 +17,7 @@ using MailKit.Net.Smtp;
using System.Security.Authentication; using System.Security.Authentication;
using Flurl.Http; using Flurl.Http;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.CodeDom.Compiler;
namespace asg_form.Controllers namespace asg_form.Controllers
{ {
@ -168,7 +169,6 @@ namespace asg_form.Controllers
/// <returns></returns> /// <returns></returns>
[Route("api/v1/enroll")] [Route("api/v1/enroll")]
[HttpPost] [HttpPost]
[Authorize]
public async Task<ActionResult<newuser_get>> Post([FromBody] Adduserreq newuser) public async Task<ActionResult<newuser_get>> Post([FromBody] Adduserreq newuser)
{ {
int wp = -1; int wp = -1;
@ -218,35 +218,43 @@ namespace asg_form.Controllers
} }
public readonly CaptchaService _captchaService = new CaptchaService(); public readonly CaptchaService _captchaService = new CaptchaService();
/// <summary>
/// 验证码生成
/// </summary>
[Route("api/v2/makeCaptcha")]
[HttpGet]
public ActionResult<(string captchaImage, string captchaCode)> GetCaptcha()
{
var captchaService = new CaptchaService();
var (captchaImage, captchaCode) = captchaService.GenerateCaptcha();
// 这里可以将验证码存储在内存缓存或数据库中,以便后续验证
//StoreCaptcha(captchaCode); // 自定义存储逻辑
return Ok(new { captchaImage, captchaCode });
}
/// <summary> /// <summary>
/// 验证码注册 /// 验证码注册
/// </summary> /// </summary>
[Route("api/v2/enroll")] [Route("api/v2/enroll")]
[HttpPost] [HttpPost]
[Authorize] public async Task<ActionResult<newuser_get>> Enroll([FromBody] AddUserReq newuser)
public async Task<ActionResult<newuser_get>> enroll([FromBody] AddUserReq newuser)
{ {
int wp = -1; int wp = -1;
try try
{ {
var maxId = await userManager.Users.MaxAsync(u => u.Id); //验证验证码的逻辑
wp = 0; //bool isCaptchaValid = ValidateCaptcha(newuser.captcha); // 自定义验证逻辑
if (true)
// 创建验证码服务实例
var captchaService = new CaptchaService();
// 验证验证码的逻辑
bool isCaptchaValid = captchaService.ValidateCaptcha(newuser.captcha); // 使用之前的验证码服务
if (!isCaptchaValid)
{ {
return BadRequest(new error_mb { code = 400, message = "验证码无效" }); return BadRequest(new { code = 400, message = "验证码无效" });
} }
User? user = await this.userManager.FindByNameAsync(newuser.userName); var maxId = await userManager.Users.MaxAsync(u => u.Id);
User? user = await userManager.FindByNameAsync(newuser.userName);
if (user == null) if (user == null)
{ {
wp = 1;
user = new User user = new User
{ {
Id = maxId + 1, Id = maxId + 1,
@ -255,16 +263,14 @@ namespace asg_form.Controllers
EmailConfirmed = true, EmailConfirmed = true,
Integral = 0 Integral = 0
}; };
wp = 3;
var r = await userManager.CreateAsync(user, newuser.password); var result = await userManager.CreateAsync(user, newuser.password);
wp = 4; if (!result.Succeeded)
if (!r.Succeeded)
{ {
return BadRequest(new error_mb return BadRequest(new error_mb
{ {
code = 400, code = 400,
message = string.Join(", ", r.Errors.Select(e => e.Description)) message = string.Join(", ", result.Errors.Select(e => e.Description))
}); });
} }
@ -283,6 +289,7 @@ namespace asg_form.Controllers
} }
public record Adduserreq(string userName, string password, string chinaname, string token); public record Adduserreq(string userName, string password, string chinaname, string token);
public record AddUserReq(string userName, string password, string chinaname, string captcha); public record AddUserReq(string userName, string password, string chinaname, string captcha);

View File

@ -5,12 +5,13 @@ using System.IO;
public class CaptchaService public class CaptchaService
{ {
private string generatedCaptchaCode; public string generatedCaptchaCode;
public int wrp = 0;
// 生成验证码图像并返回Base64字符串 // 生成验证码图像并返回Base64字符串
public (string captchaImage, string captchaCode) GenerateCaptcha() public (string captchaImage, string captchaCode) GenerateCaptcha()
{ {
generatedCaptchaCode = GenerateRandomCode(5); // 生成5位随机字符 generatedCaptchaCode = GenerateRandomCode(4); // 生成5位随机字符
wrp = 2;
Bitmap bitmap = new Bitmap(100, 40); Bitmap bitmap = new Bitmap(100, 40);
using (Graphics g = Graphics.FromImage(bitmap)) using (Graphics g = Graphics.FromImage(bitmap))
@ -21,7 +22,7 @@ public class CaptchaService
g.DrawString(generatedCaptchaCode, font, Brushes.Black, 10, 10); g.DrawString(generatedCaptchaCode, font, Brushes.Black, 10, 10);
} }
} }
wrp = 1;
using (MemoryStream ms = new MemoryStream()) using (MemoryStream ms = new MemoryStream())
{ {
bitmap.Save(ms, ImageFormat.Png); bitmap.Save(ms, ImageFormat.Png);