NeoCaptcha is a lightweight, efficient captcha generation library designed for use across multiple platforms. The library leverages SkiaSharp for rendering and provides a flexible solution for captcha creation in both .NET Core and ASP.NET Core environments.
NeoCaptcha generates captchas that can be used to validate user inputs, and includes noise elements like random lines to enhance the complexity of the captcha images.
- Multiplatform support via SkiaSharp.
- Customizable captcha text generation.
- Optional multi-color text for visual diversity.
- Simple API for integration with ASP.NET Core.
- Image format options (PNG/JPG).
- Noise generation in the form of random lines.
To use NeoCaptcha in your project, you can follow the steps outlined for both the base usage and integration with ASP.NET Core.
dotnet add package NeoCaptcha dotnet add package NeoCaptcha.AspnetCore - Add NeoCaptcha to your project via NuGet or from the source.
The core of the library is the Captcha class, which handles captcha creation. Below is how to generate a captcha image:
usingNeoCaptcha;varcaptchaOptions=newCaptchaOptions{CharacterCount=6,Width=220,Height=70,ImageFormat=CaptchaImageFormat.PNG};varcaptcha=newCaptcha(captchaOptions);Console.WriteLine($"Captcha Text: {captcha.Text}");Console.WriteLine($"Captcha Image Bytes Length: {captcha.ImageAsByteArray.Length}");This code snippet generates a captcha image with random text, which is returned as a byte array that can be rendered in a UI.
You can easily integrate NeoCaptcha into your ASP.NET Core project by registering it as a service. This allows you to generate and validate captchas in your web API.
- Add NeoCaptcha to your ASP.NET Core project via NuGet.
In the Startup.cs or Program.cs file, register NeoCaptcha as a service in the ConfigureServices method:
publicvoidConfigureServices(IServiceCollectionservices){// 5 min expirationservices.AddNeoCaptchaGenerator(TimeSpan.FromMinutes(5));}In your controller, you can now use the ICaptchaGenerator service to generate and validate captchas:
usingNeoCaptcha;usingMicrosoft.AspNetCore.Mvc;publicclassCaptchaController:ControllerBase{privatereadonlyICaptchaGenerator_captchaGenerator;publicCaptchaController(ICaptchaGeneratorcaptchaGenerator){_captchaGenerator=captchaGenerator;}[HttpGet]publicasyncTask<IActionResult>GenerateCaptcha(){varcaptcha=await_captchaGenerator.GenerateNewCaptcha();Console.WriteLine(captcha.CaptchaId);returnFile(captcha.CaptchaImage,"image/png");}[HttpPost][ServiceFilter(typeof(VerifyNeoCaptchaFilterFactory))]publicasyncTask<IActionResult>VerifyCaptchaAttribFilterFactory([FromBody]TestModelmodel){returnOk("It works!");}[HttpPost][VerifyNeoCaptcha]publicasyncTask<IActionResult>VerifyCaptchaAttrib([FromBody]TestModelmodel){returnOk("It works!");}[HttpPost]publicasyncTask<IActionResult>VerifyCaptchaManual([FromBody]TestModelmodel){varresult=await_captchaGenerator.ValidateCaptcha(model.CaptchaId,model.CaptchaChallenge);if(result==CaptchaValidationResult.OK){returnOk("It works!");}returnBadRequest("captcha error");}publicclassTestModel:NeoCaptchaCapableModel{publicrequiredstringPayload{get;set;}}}- GenerateCaptcha: This endpoint generates a new captcha and returns the image as a PNG file.
- VerifyCaptcha: This endpoint verifies the user's input against the captcha.
You can configure the captcha generation with the CaptchaOptions class:
publicclassCaptchaOptions{publicintCharacterCount{get;set;}=6;publicintWidth{get;set;}=220;publicintHeight{get;set;}=70;publicCaptchaImageFormatImageFormat{get;set;}=CaptchaImageFormat.PNG;publicboolIsMultiColorText{get;set;}=false;}- CharacterCount: The number of characters in the captcha text.
- Width: The width of the captcha image.
- Height: The height of the captcha image.
- ImageFormat: The format of the generated image (PNG or JPG).
- IsMultiColorText: Whether the captcha text should be multi-colored.
- The GenerateNewCaptcha method returns a captcha image and its associated text.
- The ValidateCaptcha method checks if the user's input matches the captcha text.
The NeoCaptchaManager is responsible for managing captcha instances, storing them temporarily, and validating user inputs:
publicclassNeoCaptchaManager:ICaptchaGenerator{privatereadonlyFastCache<Guid,string>_captchaResultHolder=new();publicTask<CaptchaGeneratorResult>GenerateNewCaptcha(CaptchaOptionscaptchaOptions){varcaptcha=newCaptcha(captchaOptions);varid=Guid.NewGuid();varresult=newCaptchaGeneratorResult(id,captcha.ImageAsByteArray);_captchaResultHolder.TryAdd(id,captcha.Text,expirationTime);returnTask.FromResult(result);}publicTask<CaptchaValidationResult>ValidateCaptcha(GuidcaptchaId,stringchallenge){if(!_captchaResultHolder.TryRemove(captchaId,outvartext)){returnTask.FromResult(CaptchaValidationResult.EXPIRED);}returnTask.FromResult(challenge==text?CaptchaValidationResult.OK:CaptchaValidationResult.INVALID);}}Make sure you have "libfontconfig1" installed :
RUN apt-get update \
&& apt-get install -y --no-install-recommends libfontconfig1 \
&& rm -rf /var/lib/apt/lists/*
AzCaptcha trying to solve captchas generated by NeoCaptcha (failing)




