G9BlazorComponent.Captcha.Component 1.0.0.7

There is a newer version of this package available.
See the version list below for details.
dotnet add package G9BlazorComponent.Captcha.Component --version 1.0.0.7                
NuGet\Install-Package G9BlazorComponent.Captcha.Component -Version 1.0.0.7                
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="G9BlazorComponent.Captcha.Component" Version="1.0.0.7" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add G9BlazorComponent.Captcha.Component --version 1.0.0.7                
#r "nuget: G9BlazorComponent.Captcha.Component, 1.0.0.7"                
#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 G9BlazorComponent.Captcha.Component as a Cake Addin
#addin nuget:?package=G9BlazorComponent.Captcha.Component&version=1.0.0.7

// Install G9BlazorComponent.Captcha.Component as a Cake Tool
#tool nuget:?package=G9BlazorComponent.Captcha.Component&version=1.0.0.7                

G9BlazorComponent.Captcha

The G9BlazorComponent.Captcha library provides a flexible and robust CAPTCHA system for .NET applications, designed to integrate seamlessly with Blazor components and ASP.NET Core web services. It consists of three main NuGet packages:

  1. G9BlazorComponent.Captcha.Common: Shared client-side and server-side logic.
  2. G9BlazorComponent.Captcha.Core: Server-side CAPTCHA generation and validation logic.
  3. G9BlazorComponent.Captcha.Component: Blazor components for integrating CAPTCHA into your UI.

Key Features

  • Support for multiple CAPTCHA types (Text-based, Click-based, and Selection-based).
  • Customizable difficulty levels and themes (light/dark).
  • Localization support for multiple languages.
  • Easy integration with .NET applications and Blazor components.
  • Configurable server-side logic for eligibility and validation.

Table of Contents


Getting Started

To use the G9BlazorComponent.Captcha library, include the required NuGet packages in your project:

  • G9BlazorComponent.Captcha.Core: For server-side logic.
  • G9BlazorComponent.Captcha.Component: For Blazor UI components.

When adding G9BlazorComponent.Captcha.Core or G9BlazorComponent.Captcha.Component, the G9BlazorComponent.Captcha.Common package is included automatically.


Usage Scenarios

Blazor Web Server Project

In a Blazor Web Server project, add both G9BlazorComponent.Captcha.Core and G9BlazorComponent.Captcha.Component to:

  1. Configure server-side CAPTCHA generation and validation.
  2. Integrate Blazor components for CAPTCHA input.

MAUI Application

For a .NET MAUI project:

  • Add G9BlazorComponent.Captcha.Component to the MAUI client application.
  • Use G9BlazorComponent.Captcha.Core on the server-side to handle CAPTCHA generation and validation requests.

Installation

Use the .NET CLI or NuGet Package Manager to install the required packages:

# Install server-side package
dotnet add package G9BlazorComponent.Captcha.Core

# Install client-side package
dotnet add package G9BlazorComponent.Captcha.Component

Configuration

Server-side

In your server project, configure the CAPTCHA generator using G9CaptchaGeneratorSingleton. Example:

builder.Services.AddSingleton(service =>
{
    var logger = service.GetService<ILogger<G9CaptchaGeneratorSingleton>>();

    var selectionBase = G9DtSelectionBaseCaptchaConfiguration.Init(new G9DtCaptchaSelectionConfig
    {
        Difficulty = G9ECaptchaDifficulty.Hard,
        Template = G9ECaptchaTheme.Dark,
        SelectionBaseCaptchaType = G9ESelectionBaseCaptchaValidationRules.AcceptOutOfOrder
    });

    var textBase = G9DtTextBaseCaptchaConfiguration.Init(new G9DtCaptchaTextBaseConfig
    {
        Difficulty = G9ECaptchaDifficulty.Hard,
        Template = G9ECaptchaTheme.Dark,
        CallbackMode = G9ECaptchaResultCallbackMode.CallbackWithConfirmButton
    });
    textBase.IsCaseSensitive = true;

    var config = new G9DtCaptchaConfiguration
    {
        CaptchaClickBaseConfig = new G9DtCaptchaClickBaseConfig
        {
            CheckEligibilityUsingClickBaseCaptcha = () => (true, null),
            UsedEligibilityOnClickBaseCaptcha = _ => true
        },
        SelectionBaseCaptchaConfiguration = selectionBase,
        TextBaseCaptchaConfiguration = textBase
    };

    return G9CaptchaGeneratorSingleton.InitOrGet(config, 180, 3600, 0, logger, false, false);
});

Map CAPTCHA API endpoints:

app.AddCaptchaApiEndpoints();

Client-side

In your Blazor component, use G9BlazorComponentCaptchaManual, G9BlazorComponentCaptchaAutomatic, or G9BlazorComponentCaptchaClient for CAPTCHA integration:

<G9BlazorComponentCaptchaClient @ref="_captcha"
                                ServerAddress="https://localhost:7255/"
                                OnApprovementCallback="OnApprovement"
                                Language="G9ECultures.en" />

Examples

Blazor Integration

A complete example of integrating CAPTCHA in a Blazor Web Server project:

Razor Component
<G9BlazorComponentCaptchaManual CaptchaMetadata="@_clientCaptchaMetadata"
                                OnRefreshRequestCallback="RefreshRequest"
                                OnResultCallback="OnCaptchaCallback"
                                Theme="G9ECaptchaTheme.Dark"
                                Language="G9ECultures.en" />
Code-behind
private G9DtCaptchaMetadata? _clientCaptchaMetadata;

protected override async Task OnInitializedAsync()
{
    _clientCaptchaMetadata = await CaptchaGeneratorSingleton.GenerateCaptcha();
}

private async Task OnCaptchaCallback(G9DtCaptchaResult result)
{
    var isValid = await CaptchaGeneratorSingleton.ValidateCaptcha(result);
    Console.WriteLine(isValid ? "CAPTCHA Valid" : "CAPTCHA Invalid");
}

MAUI Integration

Client Application

Use G9BlazorComponentCaptchaClient in your MAUI Blazor project to connect to the server-side API.


License

This library is licensed under the MIT License. See the LICENSE.md file for details.

Product Compatible and additional computed target framework versions.
.NET 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. 
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.0.9 53 1/18/2025
1.0.0.7 54 1/16/2025
1.0.0.6 57 1/16/2025
1.0.0.5 56 1/16/2025