DUWENINK.Captcha 0.9.0

dotnet add package DUWENINK.Captcha --version 0.9.0                
NuGet\Install-Package DUWENINK.Captcha -Version 0.9.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.9.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DUWENINK.Captcha --version 0.9.0                
#r "nuget: DUWENINK.Captcha, 0.9.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.9.0

// Install DUWENINK.Captcha as a Cake Tool
#tool nuget:?package=DUWENINK.Captcha&version=0.9.0                

项目基于 Hei.CaptchaHei.Captcha 的依赖注入版本

示例

中文泡泡验证码

img

1564563919705

1564563740706

字母数字组合验证码

img

1564563801717

1564563816361

1564563853298

1564563877047

表单Demo

1564564569409

如何使用

添加包

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 Compatible and additional computed target framework versions.
.NET net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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 !!!
Version Downloads Last updated
0.9.0 90 11/24/2024
0.8.0 388 8/1/2024
0.7.0 2,935 3/18/2024
0.6.0 184 3/11/2024
0.5.0 136 3/7/2024
0.4.0 132 3/7/2024
0.3.0 132 3/6/2024
0.2.0 125 3/6/2024
0.1.0 953 8/20/2019

996公司就不要用了,我会告你