图形验证码
This commit is contained in:
parent
7ca06fdfd9
commit
e4dd63f2c1
@ -216,9 +216,76 @@ namespace asg_form.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
public readonly CaptchaService _captchaService = new CaptchaService();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 验证码注册
|
||||||
|
/// </summary>
|
||||||
|
[Route("api/v2/enroll")]
|
||||||
|
[HttpPost]
|
||||||
|
[Authorize]
|
||||||
|
public async Task<ActionResult<newuser_get>> 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 token);
|
||||||
|
|
||||||
|
public record AddUserReq(string userName, string password, string chinaname, string captcha);
|
||||||
|
|
||||||
[Route("api/v1/setimg")]
|
[Route("api/v1/setimg")]
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
|
47
asg_form/Controllers/prerfunctions/Draw.cs
Normal file
47
asg_form/Controllers/prerfunctions/Draw.cs
Normal file
@ -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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user