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
                    
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="FoxLearn.AspNetCore.Captcha" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FoxLearn.AspNetCore.Captcha" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="FoxLearn.AspNetCore.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 FoxLearn.AspNetCore.Captcha --version 1.0.1
                    
#r "nuget: FoxLearn.AspNetCore.Captcha, 1.0.1"
                    
#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 FoxLearn.AspNetCore.Captcha@1.0.1
                    
#: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=FoxLearn.AspNetCore.Captcha&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=FoxLearn.AspNetCore.Captcha&version=1.0.1
                    
Install as a Cake Tool

๐Ÿ”ท 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 124 8/15/2025
1.0.0 97 7/28/2025