Example #1
0
        public async Task <bool> CheckCaptchaAsync(CaptchaParameters captchaParameters)
        {
            bool result = false;

            if (!_captchaOptions.IsCaptchaEnabled)
            {
                return(result);
            }

            foreach (var captchaField in captchaParameters.Fields)
            {
                if (captchaField.IsEnabledThreshold)
                {
                    string thresholdKeyName =
                        $"{captchaParameters.ControllerName}_{captchaParameters.ActionName}_{captchaField.FieldType.ToString()}_Threshold";

                    if (captchaField.Threshold == -1)
                    {
                        captchaField.Threshold = _captchaOptions.Threshold;
                    }
                }

                if (captchaField.IsEnabledDuration)
                {
                    string durationKeyName = $"{captchaParameters.ControllerName}_{captchaParameters.ActionName}_{captchaField.FieldType.ToString()}_Duration";

                    if (captchaField.Threshold == -1)
                    {
                        captchaField.Duration = _captchaOptions.Duration;
                    }
                }

                string cacheKeyPattern = captchaField.GetCacheKey(_captchaOptions.DomainName,
                                                                  captchaParameters.ControllerName, captchaParameters.ActionName, captchaParameters.GlobalCacheName,
                                                                  captchaParameters.IpAddress);

                int cacheVisitCount = _distributedCache.Get <int>(cacheKeyPattern);
                if (cacheVisitCount >= captchaField.Threshold)
                {
                    if (!string.IsNullOrEmpty(captchaParameters.CaptchaToken))
                    {
                        result = await CheckCaptchaTokenAsync(cacheKeyPattern, cacheVisitCount, captchaField.Duration,
                                                              captchaParameters.CaptchaToken);
                    }
                    else
                    {
                        AddToCache(cacheKeyPattern, cacheVisitCount, captchaField.Duration);
                    }
                }
                else
                {
                    AddToCache(cacheKeyPattern, cacheVisitCount, captchaField.Duration);
                }
            }

            return(result);
        }
Example #2
0
 public virtual async Task <IActionResult> ValidateCaptcha(Guid id, string solution)
 {
     try
     {
         var parameters = new CaptchaParameters
         {
             CaptchaId      = id,
             ControllerName = nameof(CaptchaController),
             ActionName     = nameof(ValidateCaptcha),
             IpAddress      = Request.HttpContext.Connection?.RemoteIpAddress?.ToString()
         };
         await _validation.ValidateAsync(parameters, solution);
     }
     catch
     {
         return(BadRequest());
     }
     return(Ok());
 }
Example #3
0
        public Task ValidateAsync(CaptchaParameters parameters, string solution)
        {
            Captcha captcha = _storage.GetCaptcha(parameters.CaptchaId);

            if (string.IsNullOrWhiteSpace(solution))
            {
                throw new CaptchaValidationException("Empty solution.");
            }
            TimeSpan diff = DateTime.Now - captcha.Created;

            if (diff > _captchaOptions.Timeout)
            {
                throw new CaptchaTimeoutException();
            }
            if (captcha.Solution != solution)
            {
                throw new CaptchaValidationException("Invalid solution.");
            }
            return(Task.CompletedTask);
        }