From 70a797c468ec129ac01694eda5eb5ee7cab121c8 Mon Sep 17 00:00:00 2001 From: Stephan van Leeuwen Date: Thu, 28 Dec 2023 11:31:59 +0100 Subject: [PATCH] Update readme how to use the IReCaptchaService directly (#63) --- AspNetCore.ReCaptcha/ValidateReCaptchaAttribute.cs | 2 +- README.md | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/AspNetCore.ReCaptcha/ValidateReCaptchaAttribute.cs b/AspNetCore.ReCaptcha/ValidateReCaptchaAttribute.cs index b2a0b0b..0b897f8 100644 --- a/AspNetCore.ReCaptcha/ValidateReCaptchaAttribute.cs +++ b/AspNetCore.ReCaptcha/ValidateReCaptchaAttribute.cs @@ -109,7 +109,7 @@ private async Task ValidateRecaptcha(ActionContext context) // Nothing to do - reCAPTCHA is not enabled / enforced. return; } - + if (!context.HttpContext.Request.HasFormContentType) { context.ModelState.AddModelError("", GetErrorMessage(context)); diff --git a/README.md b/README.md index ae57704..8b89aa5 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,12 @@ public IActionResult SubmitForm(ContactViewModel model) } ``` +### 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: ```cshtml