public async Task ValidateAsync(string captchaResponse) { var httpContext = _httpContextAccessor.HttpContext; if (httpContext == null) { throw new Exception("RecaptchaValidator should be used in a valid HTTP context!"); } if (captchaResponse.IsNullOrEmpty()) { throw new UserFriendlyException(L("CaptchaCanNotBeEmpty")); } try { await _recaptchaValidationService.ValidateResponseAsync( captchaResponse, _httpContextAccessor.HttpContext.Connection?.RemoteIpAddress?.ToString() ); } catch (RecaptchaValidationException) { throw new UserFriendlyException(L("IncorrectCaptchaAnswer")); } }
/// <inheritdoc /> public async Task OnAuthorizationAsync(AuthorizationFilterContext context) { context.CheckArgumentNull(nameof(context)); context.HttpContext.CheckArgumentNull(nameof(context.HttpContext)); if (ShouldValidate(context)) { var formField = "g-recaptcha-response"; Action invalidResponse = () => context.ModelState.AddModelError(formField, _service.ValidationMessage); try { if (!context.HttpContext.Request.HasFormContentType) { throw new RecaptchaValidationException(string.Format(Resources.Exception_MissingFormContent, context.HttpContext.Request.ContentType), false); } var form = await context.HttpContext.Request.ReadFormAsync(); var response = form[formField]; var remoteIp = context.HttpContext.Connection?.RemoteIpAddress?.ToString(); if (string.IsNullOrEmpty(response)) { invalidResponse(); return; } await _service.ValidateResponseAsync(response, remoteIp); } catch (RecaptchaValidationException ex) { _logger.ValidationException(ex.Message, ex); if (ex.InvalidResponse) { invalidResponse(); return; } else { context.Result = new BadRequestResult(); } } } }
/// <inheritdoc /> public async Task OnAuthorizationAsync(AuthorizationFilterContext context) { if (ShouldValidate(context)) { var formField = "g-recaptcha-response"; void invalidResponse() => context.ModelState.AddModelError(formField, Service.ValidationMessage); try { if (!context.HttpContext.Request.HasFormContentType) { throw new RecaptchaValidationException( $"Looks up a localized string similar to The content type is '{context.HttpContext.Request.ContentType}', it should be form content.", false); } var form = await context.HttpContext.Request.ReadFormAsync(); var response = form[formField]; var remoteIp = context.HttpContext.Connection?.RemoteIpAddress?.ToString(); if (string.IsNullOrEmpty(response)) { invalidResponse(); return; } await Service.ValidateResponseAsync(response, remoteIp); } catch (RecaptchaValidationException ex) { Logger.ValidationException(ex.Message, ex); if (ex.InvalidResponse) { invalidResponse(); return; } else { context.Result = new BadRequestResult(); } } } }
protected async Task CheckCaptchaResponseAsync(IRecaptchaValidationService recaptchaValidationService) { string response = Request.Form[RecaptchaResponseKey]; var remoteIp = HttpContext.Connection?.RemoteIpAddress?.ToString(); if (response.IsNullOrEmpty()) { throw new UserFriendlyException(L("CaptchaCanNotBeEmpty")); } try { await recaptchaValidationService.ValidateResponseAsync(response, remoteIp); } catch (RecaptchaValidationException) { throw new UserFriendlyException(L("IncorrectCaptchaAnswer")); } }