Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public bool HasRequestValidCaptchaEntry(
            Language captchaGeneratorLanguage,
            DisplayMode captchaGeneratorDisplayMode,
            DNTCaptchaBase model = null)
        {
            var httpContext = _contextAccessor.HttpContext;

            if (!shouldValidate(httpContext))
            {
                _logger.LogDebug($"Ignoring ValidateDNTCaptcha during `{httpContext.Request.Method}`.");
                return(true);
            }

            var(captchaText, inputText, cookieToken) = getFormValues(httpContext, model);

            if (string.IsNullOrEmpty(captchaText))
            {
                _logger.LogDebug("CaptchaHiddenInput is empty.");
                return(false);
            }

            if (string.IsNullOrEmpty(inputText))
            {
                _logger.LogDebug("CaptchaInput is empty.");
                return(false);
            }

            inputText = inputText.ToEnglishNumbers();

            if (!long.TryParse(
                    inputText,
                    NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands,
                    CultureInfo.InvariantCulture,
                    out long inputNumber))
            {
                _logger.LogDebug("inputText is not a number.");
                return(false);
            }

            var decryptedText = _captchaProtectionProvider.Decrypt(captchaText);

            var numberToText = _captchaTextProvider(captchaGeneratorDisplayMode).GetText(inputNumber, captchaGeneratorLanguage);

            if (decryptedText?.Equals(numberToText) != true)
            {
                _logger.LogDebug($"{decryptedText} != {numberToText}");
                return(false);
            }

            if (!isValidCookie(httpContext, decryptedText, cookieToken))
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
 public bool HasRequestValidCaptchaEntry(Language captchaGeneratorLanguage, DisplayMode captchaGeneratorDisplayMode, DNTCaptchaBase model = null)
 {
     return(true);
 }
Ejemplo n.º 3
0
        private (string captchaText, string inputText, string cookieToken) getFormValues(HttpContext httpContext, DNTCaptchaBase model)
        {
            if (httpContext.Request.HasFormContentType)
            {
                var form = httpContext.Request.Form;
                form.CheckArgumentNull(nameof(form));

                var captchaText = (string)form[DNTCaptchaTagHelper.CaptchaHiddenInputName];
                var inputText   = (string)form[DNTCaptchaTagHelper.CaptchaInputName];
                var cookieToken = (string)form[DNTCaptchaTagHelper.CaptchaHiddenTokenName];

                return(captchaText, inputText, cookieToken);
            }

            if (model == null)
            {
                throw new NullReferenceException("Your ViewModel should implement the DNTCaptchaBase class (public class AccountViewModel : DNTCaptchaBase {}).");
            }
            return(model.DNTCaptchaText, model.DNTCaptchaInputText, model.DNTCaptchaToken);
        }