VOOZH about

URL: https://www.nuget.org/packages/GoogleReCaptcha.V3/

⇱ NuGet Gallery | GoogleReCaptcha.V3 1.3.1




GoogleReCaptcha.V3 1.3.1

dotnet add package GoogleReCaptcha.V3 --version 1.3.1
 
 
NuGet\Install-Package GoogleReCaptcha.V3 -Version 1.3.1
 
 
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="GoogleReCaptcha.V3" Version="1.3.1" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="GoogleReCaptcha.V3" Version="1.3.1" />
 
Directory.Packages.props
<PackageReference Include="GoogleReCaptcha.V3" />
 
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add GoogleReCaptcha.V3 --version 1.3.1
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: GoogleReCaptcha.V3, 1.3.1"
 
 
#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.
#:package GoogleReCaptcha.V3@1.3.1
 
 
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=GoogleReCaptcha.V3&version=1.3.1
 
Install as a Cake Addin
#tool nuget:?package=GoogleReCaptcha.V3&version=1.3.1
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

GoogleReCaptcha.V3

Library for Google's ReCaptcha v3 backend https://www.google.com/recaptcha

Target platform: .NET Core 3.1+

When targeting Core 3.0 use package version 1.1.2

Interface

Task<bool> IsCaptchaPassedAsync(string token);
Task<JObject> GetCaptchaResultDataAsync(string token);
void UpdateSecretKey(string key);

Setup guide

Install the package https://www.nuget.org/packages/GoogleReCaptcha.V3/ using nuget Update your appsetting.json config file

"googleReCaptcha:SiteKey": "YOUR_SITE_KEY",
"googleReCaptcha:SecretKey": "YOUR_SECRET_KEY"
"googleReCaptcha:SecretKey": "YOUR_MINIMUM_ACCEPTABLE_SCORE"

You can get your keys here: https://www.google.com/recaptcha/admin

Update your middleware

services.AddHttpClient<ICaptchaValidator, GoogleReCaptchaValidator>();

Update your Views

@inject Microsoft.Extensions.Configuration.IConfiguration Configuration

<form asp-action="FormPost">
 <input type="hidden" name="captcha" id="captchaInput" value=""/>
 ...
</form>

@section Scripts
{
 <script src="https://www.google.com/recaptcha/api.js?render=@Configuration["googleReCaptcha:SiteKey"]"></script>
 <script>
 grecaptcha.ready(function() {
 grecaptcha.execute('@Configuration["googleReCaptcha:SiteKey"]', { action: 'contact' }).then(function (token) {
 $("#captchaInput").val(token);
 });
 });
 </script>
}

Update your controller

inject ICaptchaValidator

public class HomeController : Controller
{
 private readonly ICaptchaValidator _captchaValidator;
 public HomeController(ICaptchaValidator captchaValidator)
 {
 _captchaValidator = captchaValidator;
 }
 [HttpPost]
 public async Task<IActionResult> FormPost(Model collection, string captcha){
 if (!await _captchaValidator.IsCaptchaPassedAsync(captcha))
 {
 ModelState.AddModelError("captcha","Captcha validation failed");
 }
 if(ModelState.IsValid){
 ...
 }
 return View(collection)
 }

Additional info

You can use following method instead to get all response data

async Task<JObject> GetCaptchaResultDataAsync(string token)

To update secret key there is method.

void UpdateSecretKey(string key);

Method must be called jsut before the validation await _captchaValidator.IsCaptchaPassedAsync(captcha) after the form has been sent like this:

[HttpPost]
public async Task<IActionResult> Register(SampleViewModel collection, string captcha)
{
 _captchaValidator.UpdateSecretKey("NEW SECRET KEY");
 if (!await _captchaValidator.IsCaptchaPassedAsync(captcha))
 {
 ModelState.AddModelError("captcha","Captcha validation failed");
 }
 if(ModelState.IsValid)
 {
 ViewData["Message"] = "Success";
 }
 return View(collection);
}

Exceptions

Methods can throw 'HttpRequestException' when connection to the Google's service doesn't work or service doesn't response with HTTP 200

Contact

Contact me in case you find any errors or problem via email langerjara@gmail.com

Product Versions Compatible and additional computed target framework versions.
.NET net5.0 net5.0 was computed.  net5.0-windows net5.0-windows was computed.  net6.0 net6.0 was computed.  net6.0-android net6.0-android was computed.  net6.0-ios net6.0-ios was computed.  net6.0-maccatalyst net6.0-maccatalyst was computed.  net6.0-macos net6.0-macos was computed.  net6.0-tvos net6.0-tvos was computed.  net6.0-windows net6.0-windows was computed.  net7.0 net7.0 was computed.  net7.0-android net7.0-android was computed.  net7.0-ios net7.0-ios was computed.  net7.0-maccatalyst net7.0-maccatalyst was computed.  net7.0-macos net7.0-macos was computed.  net7.0-tvos net7.0-tvos was computed.  net7.0-windows net7.0-windows was computed.  net8.0 net8.0 was computed.  net8.0-android net8.0-android was computed.  net8.0-browser net8.0-browser was computed.  net8.0-ios net8.0-ios was computed.  net8.0-maccatalyst net8.0-maccatalyst was computed.  net8.0-macos net8.0-macos was computed.  net8.0-tvos net8.0-tvos was computed.  net8.0-windows net8.0-windows was computed.  net9.0 net9.0 was computed.  net9.0-android net9.0-android was computed.  net9.0-browser net9.0-browser was computed.  net9.0-ios net9.0-ios was computed.  net9.0-maccatalyst net9.0-maccatalyst was computed.  net9.0-macos net9.0-macos was computed.  net9.0-tvos net9.0-tvos was computed.  net9.0-windows net9.0-windows was computed.  net10.0 net10.0 was computed.  net10.0-android net10.0-android was computed.  net10.0-browser net10.0-browser was computed.  net10.0-ios net10.0-ios was computed.  net10.0-maccatalyst net10.0-maccatalyst was computed.  net10.0-macos net10.0-macos was computed.  net10.0-tvos net10.0-tvos was computed.  net10.0-windows net10.0-windows was computed. 
.NET Core netcoreapp3.1 netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on GoogleReCaptcha.V3:

Package Downloads
KaranCommon

Package Description

Dorbit.Framework

Package Description

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on GoogleReCaptcha.V3:

Repository Stars
DevBetterCom/DevBetterWeb
A simple web application for devBetter
Version Downloads Last Updated
1.3.1 344,570 5/31/2021
1.3.0 103,233 8/6/2020
1.2.0 9,976 5/28/2020
1.1.2 20,929 11/6/2019
1.1.1 1,638 11/5/2019
1.1.0 1,293 11/5/2019
1.0.0 1,429 11/5/2019

Added support for minimum score.