Google ReCAPTCHA v2/v3 Library for .NET Core 2.x/3.x/5.x.
based on 'AspNetCore.ReCaptcha' nuget.
if you using ajax calls, call updateReCaptcha()
after ajax call to get new ReCaptcha token
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.
You can install this package using NuGet. You can use the following command:
# Package Manager
PM> Install-Package MH.ReCaptcha
# .NET CLI
dotnet add package MH.ReCaptcha
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"
}
}
To use MH.ReCaptcha in your project, you must add the following code to your startup.cs:
public void ConfigureServices(IServiceCollection services) {
services.AddReCaptcha(Configuration.GetSection("ReCaptcha"));
}
In your .cshtml file you add the following using statement:
@using MH.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 *, MH.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");
}
By default, MH.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.
You can learn more about request localization in .NET Core here
For every version of .NET Core there is a configured example included in this repository. Examples are linked below for quick access: