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.
<PackageVersion Include="DUWENINK.Captcha" Version="0.9.0" />
                    
Directory.Packages.props
<PackageReference Include="DUWENINK.Captcha" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version 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.
#:package DUWENINK.Captcha@0.9.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DUWENINK.Captcha&version=0.9.0
                    
Install as a Cake Addin
#tool nuget:?package=DUWENINK.Captcha&version=0.9.0
                    
Install as a Cake Tool

项目基于 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.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
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 (2)

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.
Version Downloads Last Updated
0.9.0 324 11/24/2024
0.8.0 620 8/1/2024
0.7.0 8,236 3/18/2024
0.6.0 226 3/11/2024
0.5.0 179 3/7/2024
0.4.0 164 3/7/2024
0.3.0 166 3/6/2024
0.2.0 156 3/6/2024
0.1.0 1,105 8/20/2019

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