ASP.NET 6 集成 LazyCaptcha v2 图形验证码 您所在的位置:网站首页 asp验证码 ASP.NET 6 集成 LazyCaptcha v2 图形验证码

ASP.NET 6 集成 LazyCaptcha v2 图形验证码

2024-01-18 04:35| 来源: 网络整理| 查看: 265

1、描述:

仿EasyCaptcha和SimpleCaptcha,基于.Net Standard 2.0 的图形验证码模块,LazyCaptcha v2(基于SkiaSharp),本文采用的是在.NET 6下进行Demo演示,目前演示的是项目中常用的做法。

具体开源与扩展访问:LazyCaptcha: 仿EasyCaptcha的.net core 下的图形验证码

2、效果:

3、安装依赖

Install-Package Lazy.Captcha.Core

PS:Linux环境下运行,请安装SkiaSharp.NativeAssets.Linux包,更多细节请查看SkiaSharp官方文档。

4、注册服务 // Add services to the container. // 默认使用内存存储(AddDistributedMemoryCache) builder.Services.AddCaptcha(builder.Configuration); // 如果使用redis分布式缓存 //builder.Services.AddStackExchangeRedisCache(options => //{ // options.Configuration = builder.Configuration.GetConnectionString("RedisCache"); // options.InstanceName = "captcha:"; //});

5、配置参数

修改appsettings.json

{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*", "ConnectionStrings": { // 使用Redis缓存时,需要配置此项 // 使用格式参考 Microsoft.Extensions.Caching.StackExchangeRedis "RedisCache": "localhost,password=Aa123456." }, "CaptchaOptions": { "CaptchaType": 5, // 验证码类型 "CodeLength": 4, // 验证码长度, 要放在CaptchaType设置后 当类型为算术表达式时,长度代表操作的个数 "ExpirySeconds": 60, // 验证码过期秒数 "IgnoreCase": true, // 比较时是否忽略大小写 "StoreageKeyPrefix": "", // 存储键前缀 "ImageOption": { "Animation": false, // 是否启用动画 "FontSize": 32, // 字体大小 "Width": 100, // 验证码宽度 "Height": 40, // 验证码高度 "BubbleMinRadius": 5, // 气泡最小半径 "BubbleMaxRadius": 10, // 气泡最大半径 "BubbleCount": 3, // 气泡数量 "BubbleThickness": 1.0, // 气泡边沿厚度 "InterferenceLineCount": 4, // 干扰线数量 "FontFamily": "epilog", // 包含actionj,epilog,fresnel,headache,lexo,prefix,progbot,ransom,robot,scandal,kaiti "FrameDelay": 15, // 每帧延迟,Animation=true时有效, 默认30 "BackgroundColor": "#fff", // 格式: rgb, rgba, rrggbb, or rrggbbaa format to match web syntax, 默认#fff "ForegroundColors": "", // 颜色格式同BackgroundColor,多个颜色逗号分割,随机选取。不填,空值,则使用默认颜色集 "Quality": 100, // 图片质量(质量越高图片越大,gif调整无效可能会更大) "TextBold": false // 粗体,该配置2.0.3新增 } } }

6、创建控制器 using Lazy.Captcha.Core; using Microsoft.AspNetCore.Mvc; namespace WebAPI.LazyCaptcha.Controllers { [Route("api/[controller]/[action]")] [ApiController] public class CaptchaController : ControllerBase { private readonly ICaptcha _captcha; public CaptchaController(ICaptcha captcha) { _captcha = captcha; } [HttpGet] public IActionResult Captcha(string id) { var info = _captcha.Generate(id); // 有多处验证码且过期时间不一样,可传第二个参数覆盖默认配置。 // var info = _captcha.Generate(id, 120); using var stream = new MemoryStream(info.Bytes); return new FileContentResult(stream.ToArray(), "image/gif"); } /// /// 校验验证码是否有效 /// [HttpGet("validate")] public bool Validate(string id, string code) { return _captcha.Validate(id, code); } /// /// 多次校验(https://gitee.com/pojianbing/lazy-captcha/issues/I4XHGM) /// [HttpGet("validate2")] public bool Validate2(string id, string code) { return _captcha.Validate(id, code, false); } } }

7、生成验证码

8、效验验证码



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有