Sam.ReCaptcha 3.0.0

dotnet add package Sam.ReCaptcha --version 3.0.0                
NuGet\Install-Package Sam.ReCaptcha -Version 3.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="Sam.ReCaptcha" Version="3.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Sam.ReCaptcha --version 3.0.0                
#r "nuget: Sam.ReCaptcha, 3.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.
// Install Sam.ReCaptcha as a Cake Addin
#addin nuget:?package=Sam.ReCaptcha&version=3.0.0

// Install Sam.ReCaptcha as a Cake Tool
#tool nuget:?package=Sam.ReCaptcha&version=3.0.0                

Sam.ReCaptcha - A Powerful CAPTCHA Solution for ASP.NET Core

Sam.ReCaptcha is a robust package for implementing CAPTCHA in ASP.NET Core applications. It supports various CAPTCHA techniques, including text-based and math-based CAPTCHAs, helping to prevent automated attacks and enhance application security.


🚀 Features

  • Multiple CAPTCHA Types: Supports text and math CAPTCHAs
  • Easy Configuration: Customize fonts, colors, text size, noise effects, and background
  • Expiration Time Management: Control the validity duration of CAPTCHA
  • Case Sensitivity Control: Configure case sensitivity for input validation
  • Customizable Visual Effects: Rotate characters, add noise, and draw interference lines for enhanced security

📦 Installation

To add Sam.ReCaptcha to your project, run the following command:

 dotnet add package Sam.ReCaptcha

🔧 Usage

1. Register the CAPTCHA Service in Program.cs

First, configure Sam.ReCaptcha in Program.cs:

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing.Processing;

var builder = WebApplication.CreateBuilder(args);

// Add required services
builder.Services.AddControllers();
builder.Services.AddSwaggerGen();
builder.Services.AddDistributedMemoryCache();

// CAPTCHA Configuration
var captchaOptions = new CaptchaOptions
{
    CaptchaVariant = CaptchaTypes.DefaultCaptcha,
    MathCaptchaOptions = new MathCaptchaOptions
    {
        MinValue = 10,
        MaxValue = 20,
    },
    TextCaptchaOptions = new TextCaptchaOptions()
    {
        AllowedCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
        CodeLength = 5,
        CaseSensitivityMode = StringComparison.OrdinalIgnoreCase,
    },
    Font = ReCaptchaFonts.Timetwist,
    ExpirationTimeInMinutes = 5,

    ImageOptions = new CaptchaImageOptions
    {
        MinFontSize = 30,
        MaxFontSize = 36,
        TextColor = Color.Black,
        ShadowColor = Color.Gray,
        LetterSpacing = 30,
        ShadowPositionOffset = new PointF(2, 2),
        Rotation = new RotationOptions
        {
            MinRotation = -10,
            MaxRotation = 10
        }
    },

    LineDistortion = new LineDistortionOptions
    {
        LineColor = Color.Gray,
        LineCount = 8
    },

    NoiseEffect = new NoiseEffectOptions
    {
        NoiseDensity = 200,
        NoiseColor = Color.Silver
    },

    GradientBackground = new GradientBackgroundOptions
    {
        GradientStops =
        [
            new ColorStop(0, Color.LightBlue),
            new ColorStop(0.5f, Color.White),
            new ColorStop(1, Color.LightGray)
        ]
    }
};

// Register CAPTCHA service
builder.Services.AddCaptcha(captchaOptions);

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

2. Create an API for CAPTCHA Generation and Validation

In CaptchaController.cs, add the following endpoints for generating and validating CAPTCHAs:

using Microsoft.AspNetCore.Mvc;
using Sam.ReCaptcha.Services;

namespace Sam.ReCaptcha.WebApi.Controllers;

[ApiController]
[Route("[controller]/[action]")]
public class CaptchaController(ICaptchaService captchaService) : ControllerBase
{
    [HttpGet("{id:guid}")]
    public async Task<FileContentResult> ReCaptcha(Guid id)
    {
        var bytes = await captchaService.GenerateCaptchaImageAsync(id);
        return File(bytes, "image/jpeg");
    }

    [HttpGet("{id:guid}")]
    public async Task<bool> Validate(Guid id, [FromQuery] string code)
    {
        return await captchaService.Validate(id, code);
    }
}

🛠 Frontend Integration

1. Get CAPTCHA Image

You can retrieve the CAPTCHA image in your frontend using a GET request:

<img id="captchaImage" src="https://your-api-url.com/Captcha/ReCaptcha/{id}" />

2. Validate CAPTCHA Code

To verify the entered code, send a GET request:

GET https://your-api-url.com/Captcha/Validate/{id}?code=12345

📌 Conclusion

Sam.ReCaptcha is a powerful and flexible solution for adding CAPTCHA to ASP.NET Core applications. With extensive configuration options, visual effects, and security enhancements, this package provides an efficient way to prevent automated attacks and ensure the security of your application. 🚀

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
3.0.0 65 2/23/2025