DUWENINK.Captcha
0.8.0
dotnet add package DUWENINK.Captcha --version 0.8.0
NuGet\Install-Package DUWENINK.Captcha -Version 0.8.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DUWENINK.Captcha" Version="0.8.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DUWENINK.Captcha --version 0.8.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: DUWENINK.Captcha, 0.8.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DUWENINK.Captcha as a Cake Addin #addin nuget:?package=DUWENINK.Captcha&version=0.8.0 // Install DUWENINK.Captcha as a Cake Tool #tool nuget:?package=DUWENINK.Captcha&version=0.8.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
项目基于 Hei.Captcha 为 Hei.Captcha 的依赖注入版本
示例
中文泡泡验证码
字母数字组合验证码
表单Demo
如何使用
添加包
Install-Package DUWENINK.Captcha
添加注入(在 StartUp.cs 文件的 public void ConfigureServices(IServiceCollection services) 方法中)
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddMemoryCache();//使用缓存
services.AddDUWENINKCaptcha();//使用验证码
}
在Controller中添加注入
/// <summary>
/// 依赖注入
/// </summary>
private SecurityCodeHelper _securityCode ;
private readonly IMemoryCache _cache;
public HomeController(SecurityCodeHelper securityCode, IMemoryCache cache)
{
_securityCode = securityCode;
_cache = cache;
}
#region 生成验证码
/// <summary>
/// 泡泡中文验证码
/// </summary>
/// <returns></returns>
[HttpPost("BubbleCode")]
public IActionResult BubbleCode()
{
var code = _securityCode.GetRandomCnText(2);//生成的中文验证码
var vGuid = Guid.NewGuid().ToString();//guid
var imgbyte = _securityCode.GetBubbleCodeByte(code);//生成的中文图片
//相对于现在的过期时间 缓存相对于现在的时间后5分钟失效
_cache.Set(vGuid, code, new TimeSpan(0, 1, 0));
Response.Cookies.Append("validatecode", vGuid);//把生成的唯一Guid添加到cookie发送给前端
return File(imgbyte, "image/png");
}
/// <summary>
/// 数字字母组合验证码
/// </summary>
/// <returns></returns>
[HttpPost("HybridCode")]
public IActionResult HybridCode()
{
var code = _securityCode.GetRandomEnDigitalText(4);
var vGuid = Guid.NewGuid().ToString();//guid
var imgbyte = _securityCode.GetEnDigitalCodeByte(code);
//相对于现在的过期时间 缓存相对于现在的时间后5分钟失效
_cache.Set(vGuid, code, new TimeSpan(0, 1, 0));
Response.Cookies.Append("validatecode", vGuid);//把生成的唯一Guid添加到cookie发送给前端
return File(imgbyte, "image/png");
}
#endregion
#region 验证验证码
/// <summary>
/// 验证码验证
/// </summary>
/// <returns></returns>
[HttpPost("VerifyCode")]
public NormalResult<bool> VerifyCode([FromBody]ValidatecodeDto filter)
{
_cache.TryGetValue(filter.ValidatecodeFromCookie, out string value);
return new NormalResult<bool> { Data = string.Equals(value, filter.TextByUser, StringComparison.OrdinalIgnoreCase) };
}
#endregion
高级
参照Demo, 通过修改/丰富应用程序运行目录./fonts
目录下的字体文件,生成更多不同字体组合的验证码。
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- SixLabors.ImageSharp (>= 3.1.5)
- SixLabors.ImageSharp.Drawing (>= 2.1.4)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on DUWENINK.Captcha:
Package | Downloads |
---|---|
WalkingTec.Mvvm.Mvc
WalkingTec.Mvvm |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on DUWENINK.Captcha:
Repository | Stars |
---|---|
dotnetcore/WTM
Use WTM to write .netcore app fast !!!
|
996公司就不要用了,我会告你