FoxLearn.AspNetCore.Captcha
1.0.1
dotnet add package FoxLearn.AspNetCore.Captcha --version 1.0.1
NuGet\Install-Package FoxLearn.AspNetCore.Captcha -Version 1.0.1
<PackageReference Include="FoxLearn.AspNetCore.Captcha" Version="1.0.1" />
<PackageVersion Include="FoxLearn.AspNetCore.Captcha" Version="1.0.1" />
<PackageReference Include="FoxLearn.AspNetCore.Captcha" />
paket add FoxLearn.AspNetCore.Captcha --version 1.0.1
#r "nuget: FoxLearn.AspNetCore.Captcha, 1.0.1"
#:package FoxLearn.AspNetCore.Captcha@1.0.1
#addin nuget:?package=FoxLearn.AspNetCore.Captcha&version=1.0.1
#tool nuget:?package=FoxLearn.AspNetCore.Captcha&version=1.0.1
๐ท FoxLearn.AspNetCore.Captcha
FoxLearn.AspNetCore.Captcha is a lightweight and customizable CAPTCHA generator for ASP.NET Core applications. It helps protect your forms from automated bots by generating image-based CAPTCHA challenges.
โ Features
โ๏ธ Simple integration with ASP.NET Core
๐ Session-based CAPTCHA validation
๐ผ๏ธ Generates image CAPTCHAs on-the-fly
๐๏ธ Customizable via options
๐งฉ Supports MVC & Razor Pages
๐ฅ Installation
Install via the .NET CLI:
dotnet add package FoxLearn.AspNetCore.Captcha
Or via the NuGet UI in Visual Studio by searching for FoxLearn.AspNetCore.Captcha
๐ฆ Usage
1. Configure Services in Program.cs
or Startup.cs
var builder = WebApplication.CreateBuilder(args);
...
builder.Services.AddCaptcha();
builder.Services.AddControllersWithViews();
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(10);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
var app = builder.Build();
app.UseSession();
...
2. Create a CAPTCHA Controller
public class CaptchaController : Controller
{
private readonly ICaptchaGenerator _captchaGenerator;
public CaptchaController(ICaptchaGenerator captchaGenerator)
{
_captchaGenerator = captchaGenerator ?? throw new ArgumentNullException(nameof(captchaGenerator));
}
public FileResult Image()
{
var (imageData, text) = _captchaGenerator.GenerateCaptcha();
HttpContext.Session.SetString("CaptchaCode", text);
return File(imageData, "image/png");
}
[AcceptVerbs("Get", "Post")]
public IActionResult Validate(string captcha)
{
if (!string.IsNullOrEmpty(captcha))
{
string? code = HttpContext.Session.GetString("CaptchaCode");
if (code != null)
{
if (string.Equals(code, captcha, StringComparison.OrdinalIgnoreCase))
{
HttpContext.Session.Remove("CaptchaCode"); // Prevent replay attacks
return Json(data: true);
}
}
}
return Json(data: false);
}
}
๐งช Sample View
<form asp-controller="Home" asp-action="Submit" method="post">
<img src="/Captcha/Image" alt="Captcha" />
<input type="text" name="captcha" required />
<button type="submit">Submit</button>
</form>
๐ Validation Tips
CAPTCHA text is stored in session under "CaptchaCode".
Always remove it from session after successful validation to prevent replay attacks.
Combine with client-side validation for a better UX.
๐ License
This project is licensed under the MIT License. Free for personal and commercial use.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. 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. |
-
net6.0
- SixLabors.Fonts (>= 2.1.3)
- SixLabors.ImageSharp (>= 3.1.11)
- SixLabors.ImageSharp.Drawing (>= 2.1.7)
-
net8.0
- SixLabors.Fonts (>= 2.1.3)
- SixLabors.ImageSharp (>= 3.1.11)
- SixLabors.ImageSharp.Drawing (>= 2.1.7)
-
net9.0
- SixLabors.Fonts (>= 2.1.3)
- SixLabors.ImageSharp (>= 3.1.11)
- SixLabors.ImageSharp.Drawing (>= 2.1.7)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.