Persilsoft.Recaptcha.Blazor 1.0.9

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

// Install Persilsoft.Recaptcha.Blazor as a Cake Tool
#tool nuget:?package=Persilsoft.Recaptcha.Blazor&version=1.0.9

Persilsoft.Recaptcha.Blazor

It exposes a service that allows you to obtain a reCAPTCHA token and validate that token through a verification endpoint that you need to add to your backend.


Example

First, you need to register the following service group in the dependency container.

using Persilsoft.Recaptcha.Blazor.Options;
using ServiceCollectionExtensions;

Action<RecaptchaOptions> recaptchaOptionsConfigurator = options =>
builder.Configuration.GetSection(RecaptchaOptions.SectionKey).Bind(options);
builder.Services.AddRecaptchaServices(recaptchaOptionsConfigurator);

In the appsetting.json configuration file, you need to configure the following key:

"Recaptcha": {
        "WebApiBaseAddress": "https://persilsoft.api",
        "VerifyRecaptcha": "/recaptcha/verify",
        "SiteKey": "xxxxxxxxxxx"
    }

Where:
WebApiBaseAddress: It is the Base URL of your backend server that will expose the token verification endpoint.
VerifyRecaptcha: It is the token verification endpoint on your backend server.
SiteKey: It is your website key, also known as the reCAPTCHA key.

Now, you can use the service in your Razor page.

@page "/recaptcha"
@using Persilsoft.Recaptcha.Shared.Interfaces
@inject IRecaptchaValidatorService RecaptchaValidatorService

<h3>Demo</h3>

<EditForm Model="@model" OnValidSubmit="Send">
    <DataAnnotationsValidator />
    <div class="mb-3">
        <label for="name" class="form-label">Nombre</label>
        <input type="text" @bind="model.Name" class="form-control" id="name" name="name" />
        <ValidationMessage For="@(() => model.Name)" />
    </div>
    <div class="d-grid gap-2 col-6 mx-auto">
        <button class="btn btn-danger" type="submit">Enviar</button>
    </div>
    <hr />
    <div class="mt-3">
        <textarea disabled class="form-control">
            @result
        </textarea>
    </div>
</EditForm>

@code {
    private readonly PersonModel model = new();
    private string result;

    private async Task Send()
    {
        try
        {
            await RecaptchaValidatorService.TryVerifyInteraction();
            result = "Successful validation!!!";
        }
        catch (Exception ex)
        {
            result = ex.Message;
        }
    }

    public class PersonModel
    {
        [Required(ErrorMessage = "{0} is required.")]
        public string Name { get; set; }
    }
}

Note: The invocation of the TryVerifyInteraction method will throw an exception if the reCAPTCHA validation process fails.

Product Compatible and additional computed target framework versions.
.NET 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Persilsoft.Recaptcha.Blazor:

Package Downloads
Persilsoft.Membership.Blazor

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.9 31 5/23/2024
1.0.8 183 5/18/2024
1.0.7 89 5/18/2024
1.0.6 115 5/17/2024
1.0.5 107 5/5/2024
1.0.4 91 5/4/2024
1.0.3 46 5/3/2024
1.0.2 58 5/1/2024
1.0.1 81 4/24/2024
1.0.0 85 4/22/2024