網(wǎng)站首頁 編程語言 正文
SimpleCaptcha是一個使用簡單,基于.Net Standard 2.0的圖形驗證碼模塊。它的靈感來源于Edi.Wang的這篇文章https://edi.wang/post/2018/10/13/generate-captcha-code-aspnet-core,我將其中生成驗證碼的代碼抽取出來進行封裝得到了這個模塊。下面介紹一下使用方式。
基本使用方式
安裝SimpleCaptcha
在Nuget中搜索安裝SimpleCaptcha
安裝緩存模塊
SimpleCaptcha依賴Microsoft.Extensions.Caching.Abstractions模塊用來存儲驗證碼,所以你需要在項目中根據(jù)自己的需要安裝相應(yīng)的實現(xiàn)包,例如這里我使用Microsoft.Extensions.Caching.Memory
Startup
修改Startup.cs文件注入相應(yīng)的服務(wù):
services.AddMemoryCache() .AddSimpleCaptcha(builder => { builder.UseMemoryStore(); });
注入ICaptcha接口
在Controller中注入核心接口ICaptcha
private readonly ICaptcha _captcha; public HomeController(ICaptcha captcha) { _captcha = captcha; }
生成驗證碼
使用ICaptcha接口的Generate
方法生成驗證碼
public IActionResult Captcha(string id) { var info = _captcha.Generate(id); var stream = new MemoryStream(info.CaptchaByteData); return File(stream, "image/png"); }
驗證
使用ICaptcha接口的Validate
方法對用戶的提交進行驗證
public IActionResult Validate(string id, string code) { var result = _captcha.Validate(id, code); return Json(new { success = result }); }
完整的例子可以在這里找到:https://github.com/1992w/SimpleCaptcha/tree/master/src/SimpleCaptcha.Demo
配置
SimpleCaptcha預(yù)留了一些默認的配置項,你可以根據(jù)需要自行修改。
設(shè)置驗證碼長度
services.AddSimpleCaptcha(builder => { builder.AddConfiguration(options => { options.CodeLength = 6; }); });
設(shè)置圖片大小
services.AddSimpleCaptcha(builder => { builder.AddConfiguration(options => { options.ImageWidth = 100; options.ImageHeight = 36; }); });
設(shè)置區(qū)分大小寫
默認情況下驗證不區(qū)分大小寫
services.AddSimpleCaptcha(builder => { builder.AddConfiguration(options => { options.IgnoreCase = false; }); });
設(shè)置驗證碼有效期
驗證碼默認的有效期為5分鐘
services.AddSimpleCaptcha(builder => { builder.AddConfiguration(options => { options.ExpiryTime =TimeSpan.FromMinutes(10); }); });
設(shè)置字符集
SimpleCaptcha提供了ICaptchaCodeGenerator
接口用來生成字符,默認的實現(xiàn)是從字符集012346789ABCDEFGHIJKLMNOPQRSTUVWXYZ
中隨機生成,你可以繼承ICaptchaCodeGenerator接口實現(xiàn)自己的需求。
public class MyCaptchaCodeGenerator : ICaptchaCodeGenerator { public string Generate(int length) { throw new NotImplementedException(); } }
配置自己的生成器
services.AddSimpleCaptcha(builder => { builder.AddConfiguration(options => { options.CodeGenerator = new MyCaptchaCodeGenerator(); }); });
設(shè)置個性化的圖片
如果默認生成的圖片你覺得不符合你的要求,你可以實現(xiàn)ICaptchaImageGenerator
接口進行修改
public class CaptchaImageGenerator : ICaptchaImageGenerator { public byte[] Generate(int width, int height, string captchaCode) { throw new NotImplementedException(); } }
services.AddSimpleCaptcha(builder => { builder.AddConfiguration(options => { options.ImageGenerator = new CaptchaImageGenerator(); }); });
源代碼
所有源代碼可以在這里獲取:https://github.com/1992w/SimpleCaptcha
原文鏈接:https://www.cnblogs.com/jiangbingyang/p/13238190.html
相關(guān)推薦
- 2022-12-26 C語言實現(xiàn)十六進制轉(zhuǎn)換為十進制的方法詳解_C 語言
- 2021-11-01 redux工作原理講解及使用方法_React
- 2022-09-05 Spring是如何解決循環(huán)依賴的?
- 2022-04-10 小程序 canvas實現(xiàn)圖片預(yù)覽,圖片保存
- 2022-06-06 python可視化數(shù)據(jù)分析pyecharts初步嘗試_python
- 2022-04-18 修改taro-ui的樣式,在自定義組件中使用taro-ui,修改ui框架樣式
- 2022-06-23 C#使用DLLImport調(diào)用外部DLL的方法_C#教程
- 2022-04-03 python中如何利用matplotlib畫多個并列的柱狀圖_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支