コード例 #1
0
        /// <summary>
        /// Validates the captcha that is in the Form of the current request
        /// </summary>
        /// <param name="reportError">Lambda for reporting errors</param>
        public async Task <bool> ValidateCaptchaAsync(Action <string, string> reportError)
        {
            if (!_settings.IsValid())
            {
                _logger.LogWarning("The ReCaptcha settings are not valid");
                return(false);
            }

            // We use the header value as default if it's passed
            var reCaptchaResponse = _httpContextAccessor.HttpContext?.Request.Headers[Constants.ReCaptchaServerResponseHeaderName];

            // If this is a standard form post we get the token from the form values if not affected previously in the header
            if (String.IsNullOrEmpty(reCaptchaResponse) && (_httpContextAccessor.HttpContext?.Request.HasFormContentType ?? false))
            {
                reCaptchaResponse = _httpContextAccessor.HttpContext.Request.Form[Constants.ReCaptchaServerResponseHeaderName].ToString();
            }

            var isValid = !String.IsNullOrEmpty(reCaptchaResponse) && await VerifyCaptchaResponseAsync(reCaptchaResponse);

            if (!isValid)
            {
                reportError("ReCaptcha", S["Failed to validate captcha"]);
            }

            return(isValid);
        }
コード例 #2
0
        private ISolver CreateReCaptchaSolver(ReCaptchaSettings settings)
        {
            try
            {
                if (settings.IsValid() == false)
                {
                    return(null);
                }
                ISolver solver = new ReCaptchaV2(settings);

                return(solver);
            }
            catch (Exception ex)
            {
                App.LogWriter.WriteLog($"Can't Create Captcha Solver {ex.Message}");
                return(null);
            }
        }