public async Task <bool> IsCaptchaValid() { try { var response = await js.InvokeAsync <string>("GoogleCaptcha.GetState"); if (!string.IsNullOrWhiteSpace(response)) { var captchaValidtor = new GReCaptchaValidator() { SecretKey = captchaSettings.ReCaptchaPrivateKey, RemoteIp = httpContextAccessor.HttpContext.Connection.RemoteIpAddress?.ToString(), Response = response }; var captchaResponse = captchaValidtor.Validate(); return(captchaResponse.IsValid); } else { return(false); } } catch { return(false); } }
/// <summary> /// Validate CAPTCHA /// </summary> /// <param name="context">A context for action filters</param> /// <returns>True if CAPTCHA is valid; otherwise false</returns> protected bool ValidateCaptcha(ActionExecutingContext context) { var isValid = false; //get form values var captchaChallengeValue = context.HttpContext.Request.Form[CHALLENGE_FIELD_KEY]; var captchaResponseValue = context.HttpContext.Request.Form[RESPONSE_FIELD_KEY]; var gCaptchaResponseValue = context.HttpContext.Request.Form[G_RESPONSE_FIELD_KEY]; if ((!StringValues.IsNullOrEmpty(captchaChallengeValue) && !StringValues.IsNullOrEmpty(captchaResponseValue)) || !StringValues.IsNullOrEmpty(gCaptchaResponseValue)) { //create CAPTCHA validator var captchaValidtor = new GReCaptchaValidator(_captchaSettings.ReCaptchaVersion) { SecretKey = _captchaSettings.ReCaptchaPrivateKey, RemoteIp = context.HttpContext.Connection.RemoteIpAddress?.ToString(), Response = !StringValues.IsNullOrEmpty(captchaResponseValue) ? captchaResponseValue : gCaptchaResponseValue, Challenge = captchaChallengeValue }; //validate request var recaptchaResponse = captchaValidtor.Validate(); isValid = recaptchaResponse.IsValid; } return(isValid); }
/// <summary> /// Validate CAPTCHA /// </summary> /// <param name="context">A context for action filters</param> /// <returns>True if CAPTCHA is valid; otherwise false</returns> protected async Task <bool> ValidateCaptcha(ActionExecutingContext context) { var isValid = false; //get form values var form = await context.HttpContext.Request.ReadFormAsync(); var captchaChallengeValue = form[CHALLENGE_FIELD_KEY]; var captchaResponseValue = form[RESPONSE_FIELD_KEY]; var gCaptchaResponseValue = string.Empty; foreach (var item in form.Keys) { if (item.Contains(G_RESPONSE_FIELD_KEY_V3)) { gCaptchaResponseValue = form[item]; } } if (string.IsNullOrEmpty(gCaptchaResponseValue)) { gCaptchaResponseValue = form[G_RESPONSE_FIELD_KEY_V2]; } if ((!StringValues.IsNullOrEmpty(captchaChallengeValue) && !StringValues.IsNullOrEmpty(captchaResponseValue)) || !string.IsNullOrEmpty(gCaptchaResponseValue)) { //create CAPTCHA validator var captchaValidtor = new GReCaptchaValidator(_captchaSettings.ReCaptchaVersion) { SecretKey = _captchaSettings.ReCaptchaPrivateKey, RemoteIp = context.HttpContext.Connection.RemoteIpAddress?.ToString(), Response = !StringValues.IsNullOrEmpty(captchaResponseValue) ? captchaResponseValue.ToString() : gCaptchaResponseValue, Challenge = captchaChallengeValue }; //validate request var recaptchaResponse = await captchaValidtor.Validate(); isValid = recaptchaResponse.IsValid; if (!isValid) { foreach (var error in recaptchaResponse.ErrorCodes) { context.ModelState.AddModelError("", error); } } } return(isValid); }