# LazyCaptcha
**Repository Path**: 159238/lazy-captcha
## Basic Information
- **Project Name**: LazyCaptcha
- **Description**: 仿EasyCaptcha的.net core 下的图形验证码
- **Primary Language**: C#
- **License**: MIT
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 125
- **Created**: 2022-03-09
- **Last Updated**: 2022-03-09
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# LazyCaptcha
## 介绍
仿[EasyCaptcha](https://gitee.com/ele-admin/EasyCaptcha)和[SimpleCaptcha](https://github.com/1992w/SimpleCaptcha),基于.Net Standard 2.1的图形验证码模块。
[码云地址](https://gitee.com/pojianbing/lazy-captcha)
[Github地址](https://github.com/pojianbing/LazyCaptcha)
### 效果展示
| CaptchaType | 字体 | 静态图 | 动图 |
| --------------------- | ------- | ----------------------------------------------- | ----------------------------------------------- |
| DEFAULT (0) | Actionj |  |  |
| CHINESE (1) | kaiti |  |  |
| NUMBER (2) | Fresnel |  |  |
| NUMBER_ZH_CN (3) | kaiti |  |  |
| NUMBER_ZH_HK (4) | kaiti |  |  |
| WORD (5) | Epilog |  |  |
| WORD_LOWER (6) | Epilog |  |  |
| WORD_UPPER (7) | Epilog |  |  |
| WORD_NUMBER_LOWER (8) | Epilog |  |  |
| WORD_NUMBER_UPPER (9) | Epilog |  |  |
| ARITHMETIC (10) | Epilog |  |  |
| ARITHMETIC_ZH (11) | kaiti |  |  |
| 字体 | 图片 | 字体 | 图片 |
| ------- | ---------------------------------------- | -------- | ----------------------------------------- |
| Actionj |  | Epilog |  |
| Fresnel |  | Headache |  |
| Kaiti |  | Lexo |  |
| Prefix |  | Progbot |  |
| Ransom |  | Robot |  |
| Scandal |  |
### 在线演示(仅学习和试用,随时可能关掉服务)
``` shell
# 此次返回的是 uyfx
http://wosperry.com.cn:8006/captcha?id=999
# 更改参数为对应的ID和图形上的验证码uyfx,通过则返回true
http://wosperry.com.cn:8006/captcha/validate?id=999&code=uyfx
```
### 安装
- [Package Manager](https://www.nuget.org/packages/Lazy.Captcha.Core)
```powershell
Install-Package Lazy.Captcha.Core
```
- [.NET CLI](https://www.nuget.org/packages/Lazy.Captcha.Core)
```powershell
dotnet add package Lazy.Captcha.Core
```
### 使用说明
#### 1. 注册服务
``` csharp
// 默认使用内存存储(AddDistributedMemoryCache)
builder.Services.AddCaptcha(builder.Configuration);
// 如果使用redis分布式缓存
//builder.Services.AddStackExchangeRedisCache(options =>
//{
// options.Configuration = builder.Configuration.GetConnectionString("RedisCache");
// options.InstanceName = "captcha:";
//});
```
#### 2. 配置
##### appsettings.json (不提供配置时,使用默认配置)
``` 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
}
}
}
```
##### 代码配置
``` csharp
// 全部配置
builder.Services.AddCaptcha(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,其他字符可根据喜好设置(可能部分转字符会出现绘制不出的情况)。
});
```
#### 3. Controller
``` csharp
[ApiController]
[Route("api/captcha")]
public class CaptchaController : ControllerBase
{
public ICaptcha Captcha { get; }
public CaptchaController(ICaptcha captcha)
{
Captcha=captcha;
}
///
/// 生成
///
[HttpGet]
public IActionResult GenerateCaptcha(string id)
{
var info = Captcha.Generate(id);
var stream = new MemoryStream(info.Bytes);
return File(stream, "image/gif");
}
///
/// 校验
///
[HttpPost]
public IActionResult Validate(string id, string code)
{
return Captcha.Validate(id, code);
}
}
```