nuel_Captcha 2.0.0
dotnet add package nuel_Captcha --version 2.0.0
NuGet\Install-Package nuel_Captcha -Version 2.0.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="nuel_Captcha" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="nuel_Captcha" Version="2.0.0" />
<PackageReference Include="nuel_Captcha" />
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 nuel_Captcha --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: nuel_Captcha, 2.0.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 nuel_Captcha@2.0.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=nuel_Captcha&version=2.0.0
#tool nuget:?package=nuel_Captcha&version=2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
nuel_Captcha
A fast, lightweight, and thread-safe .NET captcha library with cached typography, SixLabors ImageSharp rendering, and asynchronous storage.
Features
- Non-blocking & Asynchronous: All database and storage operations support async/await and
CancellationToken. - Cached Typography: Font families and glyph collections are loaded and cached once at startup, eliminating per-request disk reads.
- Dependency Injection: Seamless integration via standard ASP.NET Core DI extensions.
- Storage Decoupling: Pluggable storage via
ICaptchaStore(with built-inSqlServerCaptchaStore). - Low-Resource Friendly: Housekeeping uses opportunistic, throttled cleanup without requiring background worker processes that may get killed on shared hosting or low-resource hosts.
Getting Started
1. Register Services (Program.cs)
builder.Services.AddCaptcha(options =>
{
options.ConnectionString = builder.Configuration.GetConnectionString("DefaultConnection");
options.FontPath = Path.Combine(builder.Environment.WebRootPath, "fonts/Roboto.ttf");
// Optional settings:
options.Digits = 5; // default: 5 (supported: 1-9)
options.Width = 200; // default: 200
options.Height = 60; // default: 60
options.FontSize = 30f; // default: 30
options.ForeColor = SixLabors.ImageSharp.Color.FromPixel(new SixLabors.ImageSharp.PixelFormats.Rgb24(60, 60, 60));
options.BackColor = SixLabors.ImageSharp.Color.White;
options.Expiration = TimeSpan.FromMinutes(5); // default: 5 minutes
options.CleanUpInterval = TimeSpan.FromMinutes(10); // default: 10 minutes
});
2. Generate Captcha in Controller or Minimal API
Controller Example:
public class HomeController : Controller
{
private readonly ICaptchaService _captchaService;
public HomeController(ICaptchaService captchaService)
{
_captchaService = captchaService;
}
public async Task<IActionResult> Index(CancellationToken ct)
{
var captcha = await _captchaService.GenerateAsync(ct);
ViewBag.Src = captcha.Src;
ViewBag.Code = captcha.Id;
return View();
}
}
Razor View (Index.cshtml):
<form method="post" action="/submit">
<input type="hidden" name="CaptchaCode" value="@ViewBag.Code" />
<img src="@ViewBag.Src" alt="Captcha" />
<input type="text" name="CaptchaUserInput" autocomplete="off" required />
<button type="submit">Verify</button>
</form>
Validating Captcha:
[HttpPost("/submit")]
public async Task<IActionResult> Submit(
[FromForm] string captchaCode,
[FromForm] string captchaUserInput,
CancellationToken ct)
{
bool isValid = await _captchaService.ValidateAsync(captchaCode, captchaUserInput, ct);
if (!isValid)
{
ModelState.AddModelError("CaptchaUserInput", "Invalid or expired captcha.");
return View();
}
// Process submission...
return RedirectToAction("Success");
}
Database Setup & Housekeeping
- Automatic Setup:
dbo.CaptchaCodesand indexIX_CaptchaCodes_CreationDateare automatically created on first use if not already present. - Atomic Validation: Captchas are validated and consumed atomically using UTC (
DateTime.UtcNow). Captchas older thanExpirationcannot be validated. - Opportunistic Housekeeping: Expired records are cleaned up asynchronously in the background when the store is accessed, throttled by
CleanUpIntervalto prevent load on shared hosting. You can also trigger cleanup manually:await captchaService.CleanUpAsync(ct);
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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.
-
net10.0
- Microsoft.Data.SqlClient (>= 7.0.2)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.11)
- Microsoft.Extensions.Options (>= 10.0.11)
- SixLabors.Fonts (>= 3.1.0)
- SixLabors.ImageSharp (>= 4.1.1)
- SixLabors.ImageSharp.Drawing (>= 3.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.