AspNetCore.ReCaptcha 1.8.2

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

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

AspNetCore.ReCaptcha

NuGet Version Coverage Status

ReCAPTCHA Library for .NET 6 and above.

Requirements

This package requires a secret key as well as a site key provided by ReCaptcha. You can aquire your keyset at https://www.google.com/recaptcha/intro/v3.html. It's possible to use either v2 or v3 ReCaptcha.

Installation

You can install this package using NuGet. You can use the following command:

# Package Manager
PM> Install-Package AspNetCore.ReCaptcha

# .NET CLI
dotnet add package AspNetCore.ReCaptcha

Configuration

Place the aquired secret key and site key in the appsettings.json of your project. An example of the appsettings file is below:

{
    "ReCaptcha": {
        "SiteKey": "your site key here",
        "SecretKey": "your secret key here",
        "Version": "v2", // The ReCaptcha version to use, can be v2, v2invisible or v3
        "UseRecaptchaNet": false, // Value whether to use google recaptcha or recaptcha.net
        "ScoreThreshold": 0.5, // Only applicable for recaptcha v3, specifies the score threshold when it is considered successful
    }
}

Usage

To use AspNetCore.ReCaptcha in your project, you must add the ReCaptcha services to the service container like so:

builder.Services.AddReCaptcha(builder.Configuration.GetSection("ReCaptcha"));

In your .cshtml file you add the following using statement:

@using AspNetCore.ReCaptcha

And then you can add the ReCaptcha element to your DOM using the following code or make use of the tag-helper:

@Html.ReCaptcha()
<recaptcha />

To be able to make use of the taghelper, you will need to include the following line of code in your _ViewImports.cshtml:

@addTagHelper *, AspNetCore.ReCaptcha

The action that you will be posting to (in this case SubmitForm) will need the following attribute on the method:

[ValidateReCaptcha]
[HttpPost]
public IActionResult SubmitForm(ContactViewModel model)
{
    if (!ModelState.IsValid)
        return View("Index");

    TempData["Message"] = "Your form has been sent!";
    return RedirectToAction("Index");
}

Manual Validation

If you need to validate a recaptcha but cannot use the [ValidateReCaptcha] attribute for some reason (maybe you're using a JSON POST instead of form POST). In that case you can inject the AspNetCore.ReCaptcha.IReCaptchaService service and use one of the two methods on there: VerifyAsync verifies the provided token against the recaptcha service and returns whether or not it is successful. For V3 this also checks if the score is greater than or equal to the ScoreThreshold that can be configured in the appsettings. GetVerifyResponseAsync calls the recaptcha service with the provided token and returns the response. The response will contain whether it is successful or not and for V3 what the score is.

Language support

By default, AspNetCore.ReCaptcha will use the language that is being used in the request. So we will make use of the Culture of the HttpContext. However, you can override this by specifying a language in the ReCaptcha element. This is shown in the next example:

@Html.ReCaptcha(language: "en-GB")
<recaptcha language="en-GB" />

We support all languages supported by ReCaptcha, list can be found here.

This will only change the language of the recaptcha widget that is shown, not the language of the message that is shown when the recaptcha failed.

To localize the error message (e.g. the message set with [ValidateReCaptcha(ErrorMessage = "")]), add the message to the resource file of the controller or razor page that the validate attribute is used. For an example of this check the Razor Pages sample and look at the Pages/ContactModel.cs and Resources/Pages/ContactModel.resx files.

You can learn more about request localization in ASP.NET Core here

Examples

The two sample projects show two different use cases of using the library. One is using the MVC pattern, the other uses the Razor Pages pattern. The sample with Razor Pages is also configured with request localization.

MVC

Razor Pages

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 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. 
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.8.2 26,218 7/20/2024
1.8.1 74,765 1/10/2024
1.8.0 5,450 12/28/2023
1.7.0 116,839 2/2/2023
1.6.0 85,294 9/28/2022
1.5.4 24,476 8/4/2022
1.5.3 41,435 3/16/2022
1.5.2 10,944 2/11/2022
1.5.1 1,624 2/3/2022
1.4.0 7,229 1/9/2022
1.3.0 2,841 12/11/2021
1.2.5 26,861 9/30/2021
1.2.3 17,008 7/20/2021
1.2.2 9,011 6/5/2021
1.2.1 14,149 3/15/2021
1.2.0 5,715 3/1/2021
1.1.0 2,087 4/24/2020
1.0.0 583 4/17/2020