RuoVea.ExCaptcha 图形验证码帮助类库

RuoVea.ExCaptcha 图形验证码帮助类库
最新回答
青春明媚╮仍带伤

2026-06-21 05:11:10

RuoVea.ExCaptcha 是一个基于.NET Standard 2.0的图形验证码模块,支持仿EasyCaptcha和SimpleCaptcha的功能。以下是关于该库的详细说明:

  1. 版本说明:

    v1:基于ImageSharp,适用于不需要高性能的场景。发布到Linux时无需额外安装NativeAssets。

    v2:基于SkiaSharp,性能更好,但发布到Linux时需要安装对应的NativeAssets。

  2. 快速入门:

    通过NuGet安装组件:

    Install-Package RuoVea.ExCaptcha

    注入服务:

    service.AddExCaptcha(builder.Configuration);
  3. 使用示例:

    获取验证码:

    private readonly ICaptcha _captcha;[AllowAnonymous][DisplayName("获取验证码")]public dynamic GetCaptcha(){ var codeId = RuoVea.ExIdGen.IdGenerator.Id; var captcha = _captcha.Generate(codeId.ToString()); return new { Id = codeId, Img = captcha.Base64 };}

    验证验证码:

    if (!_captcha.Validate(input.CodeId.ToString(), input.Code))
  4. 配置说明:

    CaptchaType:设置验证码内容类型,包括中文汉字、数字、字母大小写等。

    CodeLength:验证码内容长度,数字运算时为等号左侧的数字个数。

    ExpirySeconds:验证码过期时间(秒)。

    IgnoreCase:验证时是否忽略大小写。

    Animation:是否生成GIF动态图。

    FontSize:验证码字体大小。

    FontFamily:验证码字体名称。

    TextBold:验证码内容是否加粗。

    Width/Height:验证码图片的宽高。

    BubbleCount/BubbleThickness/BubbleMinRadius/BubbleMaxRadius:气泡相关配置。

    InterferenceLineCount:干扰线数量。

    FrameDelay:GIF图片每帧的间隔(毫秒)。

  5. 配置示例:

    appsettings.json:

    { "ConnectionStrings": { "RedisCache": "localhost,password=Aa123456." }, "CaptchaOptions": { "CaptchaType": 5, "CodeLength": 4, "ExpirySeconds": 60, "IgnoreCase": true, "StoreageKeyPrefix": "", "ImageOption": { "Animation": false, "FontSize": 32, "Width": 100, "Height": 40, "BubbleMinRadius": 5, "BubbleMaxRadius": 10, "BubbleCount": 3, "BubbleThickness": 1.0, "InterferenceLineCount": 3, "FontFamily": "kaiti", "FrameDelay": 15, "BackgroundColor": "#ffffff", "ForegroundColors": "", "Quality": 100, "TextBold": false } }}

    代码配置:

    builder.Services.AddCaptcha(builder.Configuration, option =>{ option.CaptchaType = CaptchaType.WORD; option.CodeLength = 6; option.ExpirySeconds = 30; option.IgnoreCase = true; option.StoreageKeyPrefix = ""; option.ImageOption.Animation = true; option.ImageOption.FrameDelay = 30; option.ImageOption.Width = 150; option.ImageOption.Height = 50; option.ImageOption.BackgroundColor = SixLabors.ImageSharp.Color.White; option.ImageOption.BubbleCount = 2; option.ImageOption.BubbleMinRadius = 5; option.ImageOption.BubbleMaxRadius = 15; option.ImageOption.BubbleThickness = 1; option.ImageOption.InterferenceLineCount = 2; option.ImageOption.FontSize = 36; option.ImageOption.FontFamily = DefaultFontFamilys.Instance.Actionj; option.ImageOption.TextBold = true;});
  6. 控制器示例:

    [Route("captcha")][ApiController]public class CaptchaController : Controller{ private readonly ICaptcha _captcha; public CaptchaController(ICaptcha captcha) { _captcha = captcha; } [HttpGet] public IActionResult Captcha(string id) { var info = _captcha.Generate(id); var stream = new MemoryStream(info.Bytes); return File(stream, "image/gif"); } [HttpGet("validate")] public bool Validate(string id, string code) { return _captcha.Validate(id, code); } [HttpGet("validate2")] public bool Validate2(string id, string code) { return _captcha.Validate(id, code, false); }}
  7. 自定义随机验证码:

    RandomCaptcha类:

    public class RandomCaptcha : DefaultCaptcha{ private static readonly Random random = new(); private static readonly CaptchaType[] captchaTypes = Enum.GetValues<CaptchaType>(); public RandomCaptcha(IOptionsSnapshot<CaptchaOptions> options, IStorage storage) : base(options, storage) { } protected override void ChangeOptions(CaptchaOptions options) { options.CaptchaType = captchaTypes[random.Next(0, captchaTypes.Length)]; if (options.CaptchaType.IsArithmetic()) { options.CodeLength = 2; } else { options.CodeLength = 4; } if (options.CaptchaType.ContainsChinese()) { options.ImageOption.FontFamily = DefaultFontFamilys.Instance.Kaiti; options.ImageOption.FontSize = 24; } else { options.ImageOption.FontFamily = DefaultFontFamilys.Instance.Actionj; } options.ImageOption.Animation = random.Next(2) == 0; options.ImageOption.InterferenceLineCount = random.Next(1, 4); options.ImageOption.BubbleCount = random.Next(1, 4); }}

    注入RandomCaptcha:

    builder.Services.AddCaptcha(builder.Configuration);// builder.Services.Add(ServiceDescriptor.Scoped<ICaptcha, RandomCaptcha>());

通过以上配置和使用示例,您可以灵活地集成和使用RuoVea.ExCaptcha库来生成和验证图形验证码。