介绍
LazyCaptcha是仿EasyCaptcha和SimpleCaptcha,基于.Net Standard 2.1的图形验证码模块。
项目地址
效果展示:https://gitee.com/pojianbing/lazy-captcha.
| CaptchaType | 字体 | 静态图 | 动图 |
|---|---|---|---|
| DEFAULT | Actionj | ![]() |
![]() |
| WORD | Actionj | ![]() |
![]() |
| WORD_LOWER | Actionj | ![]() |
![]() |
| WORD_UPPER | Actionj | ![]() |
![]() |
| WORD_NUMBER_LOWER | Actionj | ![]() |
![]() |
| WORD_NUMBER_UPPER | Actionj | ![]() |
![]() |
| NUMBER | Fresnel | ![]() |
![]() |
| NUMBER_ZH_CN | kaiti | ![]() |
![]() |
| NUMBER_ZH_HK | kaiti | ![]() |
![]() |
| ARITHMETIC | Actionj | ![]() |
![]() |
| ARITHMETIC_ZH | Actionj | ![]() |
![]() |
| 字体 | 图片 |
|---|---|
| Actionj | ![]() |
| Epilog | ![]() |
| Fresnel | ![]() |
| Headache | ![]() |
| Kaiti | ![]() |
| Lexo | ![]() |
| Prefix | ![]() |
| Progbot | ![]() |
| Ransom | ![]() |
| Robot | ![]() |
| Scandal | ![]() |
安装教程
Install-Package Lazy.Captcha.Core
使用说明
-
注册服务
默认设置
builder.Services.AddDistributedMemoryCache().AddCaptcha();
详细设置
builder.Services.AddDistributedMemoryCache().AddCaptcha(option =>{option.CaptchaType = CaptchaType.DEFAULT; // 验证码类型option.CodeLength = 4; // 验证码长度, 要放在CaptchaType设置后option.ExpiryTime = TimeSpan.FromSeconds(30); // 验证码过期时间option.IgnoreCase = true; // 比较时是否忽略大小写option.ImageOption.Animation = false; // 是否启用动画option.ImageOption.Width = 130; // 验证码宽度option.ImageOption.Height = 48; // 验证码高度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 = 28; // 字体大小option.ImageOption.FontFamily = DefaultFontFamilys.Instance.Actionj; // 字体,中文使用kaiti,其他字符可根据喜好设置(可能部分转字符会出现绘制不出的情况)。});
-
Controller
public class CaptchaController : Controller{private readonly ILogger<CaptchaController> _logger;private readonly ICaptcha _captcha;public CaptchaController(ILogger<CaptchaController> logger, ICaptcha captcha){_logger = logger;_captcha = captcha;}[HttpGet][Route("/captcha")]public IActionResult Captcha(string id){var info = _captcha.Generate(id);var stream = new MemoryStream(info.Bytes);return File(stream, "image/gif");}[HttpGet][Route("/captcha/validate")]public bool Validate(string id, string code){if (!_captcha.Validate(id, code)){throw new Exception("无效验证码");}// 具体业务// 为了演示,这里仅做返回处理return true;}}
项目地址:https://gitee.com/pojianbing/lazy-captcha
































