![]() |
VOOZH | about |
dotnet add package DUWENINK.Captcha --version 0.10.2
NuGet\Install-Package DUWENINK.Captcha -Version 0.10.2
<PackageReference Include="DUWENINK.Captcha" Version="0.10.2" />
<PackageVersion Include="DUWENINK.Captcha" Version="0.10.2" />Directory.Packages.props
<PackageReference Include="DUWENINK.Captcha" />Project file
paket add DUWENINK.Captcha --version 0.10.2
#r "nuget: DUWENINK.Captcha, 0.10.2"
#:package DUWENINK.Captcha@0.10.2
#addin nuget:?package=DUWENINK.Captcha&version=0.10.2Install as a Cake Addin
#tool nuget:?package=DUWENINK.Captcha&version=0.10.2Install as a Cake Tool
添加包
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 | net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
Showing the top 2 NuGet packages that depend on DUWENINK.Captcha:
| Package | Downloads |
|---|---|
|
WalkingTec.Mvvm.Mvc
WalkingTec.Mvvm |
|
|
Xen.Mvc
Xen |
Showing the top 2 popular GitHub repositories that depend on DUWENINK.Captcha:
| Repository | Stars |
|---|---|
|
dotnetcore/WTM
Use WTM to write .netcore app fast !!!
|
|
|
iioter/iotgateway
An industrial IoTGateway with B/S architecture that enables bidirectional communication between industrial devices (southbound connections) and IoT platforms (northbound connections). It supports numerous industrial protocols, and can connect to various IoT cloud platforms.
|
996公司就不要用了,我会告你