KITT.Web.ReCaptcha.Http 1.0.0

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

// Install KITT.Web.ReCaptcha.Http as a Cake Tool
#tool nuget:?package=KITT.Web.ReCaptcha.Http&version=1.0.0                

KITT.Web.ReCaptcha.Http

This project add Google reCaptcha to your ASP.NET Core apps giving the service to validate your reCaptcha client response.<br/> This project targets .NET 6 and .NET 8 as supported Framework versions.

Installation

This project is available on NuGet.

It can be installed using the dotnet add package command or the NuGet wizard on your favourite IDE.

  dotnet add package KITT.Web.ReCaptcha.Http

reCaptcha v2

Usage

The project gives you an HttpClient service which expose a VerifyAsync method to verify the reCaptcha response send by the user from the client.

Add the namespace KITT.Web.ReCaptcha.Http.v2 to your Program.cs and use the AddReCaptchaV2HttpClient extension method to your IServiceCollection instance:

builder.Services.AddReCaptchaV2HttpClient(options =>
{
    options.SecretKey = "<your reCaptcha server-side secret key>";
});

Then you can inject the ReCaptchaService class whenever you need and call the VerifyAsync like this:

app.MapPost("/send", async (ReCaptchaService reCaptchaService, [FromBody] SendRequest request) =>
{
    // Here you call the reCaptcha server-side validation
    var captchaResponse = await reCaptchaService.VerifyAsync(request.CaptchaResponse);
    if (!captchaResponse.Success)
    {
        return Results.BadRequest(captchaResponse.ErrorCodes);
    }

    return Results.Ok();
});

Methods

The VerifyAsync method has the following input parameters:

Property Description
response (Required) string: The user response token provided by the reCAPTCHA client-side integration on your site.
remoteIp (Optional) string: The user's IP address. (Default: null)
cancellationToken (Optional) CancellationToken: a cancellation token instance (Default: CancellationToken.None)

The method returns an instance of the ReCaptchaResponse class, which have the following properties:

Property Description
Success bool: whether the verification ended successfully
ChallengeTimestamp DateTime: the timestamp of the challenge load
Hostname string: the hostname of the site where the reCAPTCHA was solved
ErrorCodes IEnumerable<string>: the optional list of error codes (see Google's official documentation)

reCaptcha v3

Usage

The project gives you an HttpClient service which expose a VerifyAsync method to verify the reCaptcha response send by the user from the client.

Add the namespace KITT.Web.ReCaptcha.Http.v3 to your Program.cs and use the AddReCaptchaV3HttpClient extension method to your IServiceCollection instance:

builder.Services.AddReCaptchaV3HttpClient(options =>
{
    options.SecretKey = "<your reCaptcha server-side secret key>";
});

Then you can inject the ReCaptchaService class whenever you need and call the VerifyAsync like this:

app.MapPost("/send", async (ReCaptchaService reCaptchaService, [FromBody] SendRequest request) =>
{
    // Here you call the reCaptcha server-side validation
    var captchaResponse = await reCaptchaService.VerifyAsync(request.CaptchaResponse, request.Action);
    if (!captchaResponse.Success)
    {
        return Results.BadRequest(captchaResponse.ErrorCodes);
    }

    return Results.Ok();
});

Methods

The VerifyAsync method has the following input parameters:

Property Description
response (Required) string: The user response token provided by the reCAPTCHA client-side integration on your site.
action (Required) string: The action value used to configure the reCaptcha
remoteIp (Optional) string: The user's IP address. (Default: null)
cancellationToken (Optional) CancellationToken: a cancellation token instance (Default: CancellationToken.None)

The method returns an instance of the ReCaptchaResponse class, which have the following properties:

Property Description
Success bool: whether the verification ended successfully
Score double: the score for the request (from 0.0 to 1.0)
Action string: the action name for this request
ChallengeTimestamp DateTime: the timestamp of the challenge load
Hostname string: the hostname of the site where the reCAPTCHA was solved
ErrorCodes IEnumerable<string>: the optional list of error codes (see Google's official documentation)
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. 
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 91 10/29/2024
1.0.0 233 12/28/2023
0.2.0 167 12/2/2023
0.1.0 192 8/21/2023