/// <inheritdoc /> public async Task OnAuthorizationAsync(AuthorizationFilterContext context) { context.CheckArgumentNull(nameof(context)); Action invalidResponse = () => context.ModelState.AddModelError(_configurationService.FormField, _service.ValidationMessage); await _service.ValidateAsync(context.HttpContext); try { context.HttpContext.CheckArgumentNull(nameof(context.HttpContext)); } catch (RecaptchaValidationException ex) { _logger.ValidationException(ex.Message, ex); if (ex.InvalidResponse) { invalidResponse(); return; } else { context.Result = new BadRequestResult(); } } }
public async Task <(bool isValid, string errorMessage)> Validate(HttpContext context) { context.CheckArgumentNull(nameof(context)); if (ShouldValidate(context)) { var formField = "g-recaptcha-response"; try { if (!context.Request.HasFormContentType) { throw new RecaptchaValidationException(string.Format(Resources.MainResource.Exception_MissingFormContent, context.Request.ContentType), false); } var form = await context.Request.ReadFormAsync(); var response = form[formField]; var remoteIp = context.Connection?.RemoteIpAddress?.ToString(); if (string.IsNullOrEmpty(response)) { return(false, "Not verified human!"); } var result = await _service.ValidateAsync(response, remoteIp); if (result.Success) { return(result.Success, ""); } return(result.Success, string.Join(", ", result.ErrorCodes)); } catch (RecaptchaValidationException ex) { _logger.ValidationException(ex.Message, ex); return(ex.InvalidResponse, ex.Message); } } else { return(true, "No validation is needed!"); } }