CSharp-Single-EFCore/Bunny.WebApi/Controllers/CaptchaTestController.cs

53 lines
1.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Bunny.Dao.Common.Constant.Result;
using Bunny.Dao.Common.Result;
using Lazy.Captcha.Core;
using log4net;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Mvc;
namespace Bunny.WebApi.Controllers;
/// <summary>
/// 验证码测试
/// 2024-9-11 09:02:18
/// </summary>
[Microsoft.AspNetCore.Components.Route("/api/[controller]/[action]")]
public class CaptchaTestController : ControllerBase
{
private static readonly ILog Log = LogManager.GetLogger(typeof(CaptchaTestController));
[Inject] public required ICaptcha Captcha { get; set; }
/// <summary>
/// 测试测试验证码生成
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult GetCaptcha(string id)
{
var captchaData = Captcha.Generate(id);
// 验证码的ID
var captchaDataId = captchaData.Id;
// 验证码的代码
var captchaDataCode = captchaData.Code;
Log.Debug($"验证码的ID{captchaDataId};验证码的代码:{captchaDataCode}");
// 返回验证码图片内容
var stream = new MemoryStream(captchaData.Bytes);
return File(stream, "image/gif");
}
/// <summary>
/// 输入验证码机型校验
/// </summary>
/// <returns></returns>
[HttpPost]
public Result<string> CheckCode(string id, string code)
{
// 校验用户输入的验证码是否=正确
var validate = Captcha.Validate(id, code);
return validate
? Result<string>.Success()
: Result<string>.Error(null!, ErrorConstant.ValidateCodeError);
}
}