From e4dd63f2c1f1d3ff855ece9b3ef77ebacdf95bff 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 15:04:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E5=BD=A2=E9=AA=8C=E8=AF=81=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- asg_form/Controllers/login.cs | 67 ++++++++++++++++++++++ asg_form/Controllers/prerfunctions/Draw.cs | 47 +++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 asg_form/Controllers/prerfunctions/Draw.cs diff --git a/asg_form/Controllers/login.cs b/asg_form/Controllers/login.cs index f93b879..e4a8968 100644 --- a/asg_form/Controllers/login.cs +++ b/asg_form/Controllers/login.cs @@ -216,9 +216,76 @@ namespace asg_form.Controllers } } + public readonly CaptchaService _captchaService = new CaptchaService(); + + /// + /// 验证码注册 + /// + [Route("api/v2/enroll")] + [HttpPost] + [Authorize] + public async Task> enroll([FromBody] AddUserReq newuser) + { + int wp = -1; + + try + { + var maxId = await userManager.Users.MaxAsync(u => u.Id); + wp = 0; + + // 创建验证码服务实例 + var captchaService = new CaptchaService(); + + // 验证验证码的逻辑 + bool isCaptchaValid = captchaService.ValidateCaptcha(newuser.captcha); // 使用之前的验证码服务 + if (!isCaptchaValid) + { + return BadRequest(new error_mb { code = 400, message = "验证码无效" }); + } + + User? user = await this.userManager.FindByNameAsync(newuser.userName); + if (user == null) + { + wp = 1; + user = new User + { + Id = maxId + 1, + UserName = newuser.userName, + chinaname = newuser.chinaname, + EmailConfirmed = true, + Integral = 0 + }; + wp = 3; + + var r = await userManager.CreateAsync(user, newuser.password); + wp = 4; + if (!r.Succeeded) + { + return BadRequest(new error_mb + { + code = 400, + message = string.Join(", ", r.Errors.Select(e => e.Description)) + }); + } + + return Ok(new { code = 200, message = "注册成功!" }); + } + else + { + return BadRequest(new error_mb { code = 400, message = "邮箱已被注册" }); + } + } + catch (Exception ex) + { + var innerException = ex.InnerException != null ? ex.InnerException.Message : ex.Message; + return Ok(new { code = 500, message = "服务器错误", details = innerException, wp }); + } + } + public record Adduserreq(string userName, string password, string chinaname, string token); + public record AddUserReq(string userName, string password, string chinaname, string captcha); [Route("api/v1/setimg")] [HttpPost] diff --git a/asg_form/Controllers/prerfunctions/Draw.cs b/asg_form/Controllers/prerfunctions/Draw.cs new file mode 100644 index 0000000..8176a24 --- /dev/null +++ b/asg_form/Controllers/prerfunctions/Draw.cs @@ -0,0 +1,47 @@ +using System; +using System.Drawing; +using System.Drawing.Imaging; +using System.IO; + +public class CaptchaService +{ + private string generatedCaptchaCode; + + // 生成验证码图像并返回Base64字符串 + public (string captchaImage, string captchaCode) GenerateCaptcha() + { + generatedCaptchaCode = GenerateRandomCode(5); // 生成5位随机字符 + Bitmap bitmap = new Bitmap(100, 40); + + using (Graphics g = Graphics.FromImage(bitmap)) + { + g.Clear(Color.White); + using (Font font = new Font("Arial", 20)) + { + g.DrawString(generatedCaptchaCode, font, Brushes.Black, 10, 10); + } + } + + using (MemoryStream ms = new MemoryStream()) + { + bitmap.Save(ms, ImageFormat.Png); + string base64Image = Convert.ToBase64String(ms.ToArray()); + return (base64Image, generatedCaptchaCode); + } + } + + // 验证用户输入的验证码 + public bool ValidateCaptcha(string userInput) + { + return userInput.Equals(generatedCaptchaCode, StringComparison.OrdinalIgnoreCase); + } + + // 生成随机字符 + private string GenerateRandomCode(int length) + { + const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + Random random = new Random(); + return new string(Enumerable.Repeat(chars, length) + .Select(s => s[random.Next(s.Length)]).ToArray()); + } +}