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

52 lines
1.6 KiB
C#
Raw Normal View History

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>
/// 验证码测试
/// </summary>
[Microsoft.AspNetCore.Mvc.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);
}
}