仿EasyCaptcha和SimpleCaptcha,基于.Net Standard 2.1的图形验证码模块。
字体 | 图片 |
---|---|
Actionj | ![]() |
Epilog | ![]() |
Fresnel | ![]() |
Headache | ![]() |
Kaiti | ![]() |
Lexo | ![]() |
Prefix | ![]() |
Progbot | ![]() |
Ransom | ![]() |
Robot | ![]() |
Scandal | ![]() |
# 此次返回的是 uyfx
http://wosperry.com.cn:8006/captcha?id=999
# 更改参数为对应的ID和图形上的验证码uyfx,通过则返回true
http://wosperry.com.cn:8006/captcha/validate?id=999&code=uyfx
# 这里需要调整,发布后调整为对应的具体实现
Install-Package Lazy.Captcha.Core -Version 1.0.7
dotnet add package Lazy.Captcha.Core --version 1.0.7
# 如果使用redis存储需要安装
Install-Package Lazy.Captcha.Redis -Version 1.0.7
dotnet add package Lazy.Captcha.Redis --version 1.0.7
- 注册服务(选择其一即可)
// 内存缓存
builder.Services.AddMemoryCacheCaptcha(builder.Configuration);
// Redis缓存
builder.Services.AddRedisCacheCaptcha(builder.Configuration);
- 配置
appsettings.json (不提供配置时,使用默认配置)
{
"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": "kaiti" // 包含中文时请使用kaiti。可设置字体:actionj,epilog,fresnel,headache,lexo,prefix,progbot,ransom,robot,scandal,kaiti
}
}
}
代码配置
// 全部配置
builder.Services.AddMemoryCacheCaptcha(builder.Configuration, option =>
{
option.CaptchaType = CaptchaType.WORD; // 验证码类型
option.CodeLength = 4; // 验证码长度, 要放在CaptchaType设置后 当类型为算术表达式时,长度代表操作的个数
option.ExpirySeconds = 30; // 验证码过期时间
option.IgnoreCase = true; // 比较时是否忽略大小写
option.StoreageKeyPrefix= ""; // 存储键前缀
option.ImageOption.Animation = true; // 是否启用动画
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.Scandal; // 字体,中文使用kaiti,其他字符可根据喜好设置(可能部分转字符会出现绘制不出的情况)。
});
appsettings.json配置和代码配置同时设置时,代码配置会覆盖appsettings.json配置。
- Controller
[ApiController]
[Route("api/captcha")]
public class CaptchaController : ControllerBase
{
public ICaptcha Captcha { get; }
public CaptchaController(ICaptcha captcha)
{
Captcha=captcha;
}
/// <summary>
/// 生成
/// </summary>
[HttpGet]
public IActionResult GenerateCaptcha(string id)
{
var info = Captcha.Generate(id);
var stream = new MemoryStream(info.Bytes);
return File(stream, "image/gif");
}
/// <summary>
/// 校验
/// </summary>
[HttpPost]
public IActionResult Validate(string id, string code)
{
var pass = Captcha.Validate(id, code);
if (pass)
{
// 业务
return Ok("验证码校验通过");
}
else
{
return Unauthorized("验证码校验未通过");
}
}
}