public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var httpContext = filterContext.HttpContext;
            var response    = httpContext.Response;
            var cache       = httpContext.Cache;
            var user        = httpContext.User;

            if (!user.IsInRole("Admin"))
            {
                var key      = user.Identity.ToString();
                var throttle = cache[key] as ThrottleContext;

                if (throttle == null)
                {
                    throttle = new ThrottleContext(Limit, _timeout);
                    cache.Insert(key, throttle, null, Cache.NoAbsoluteExpiration, _timeout);
                }

                var success = throttle.Increment();

                response.AppendHeader("X-RateLimit-Limit", Limit.ToString());
                response.AppendHeader("X-RateLimit-Remaining", (Limit - throttle.Count).ToString());
                response.AppendHeader("X-RateLimit-Timeout", _timeout.ToString());

                if (!success)
                {
                    HandleRateLimitedRequest(filterContext);
                }
            }

            base.OnActionExecuting(filterContext);
        }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var httpContext = filterContext.HttpContext;
            var response = httpContext.Response;
            var cache = httpContext.Cache;
            var user = httpContext.User;

            if (!user.IsInRole("Admin"))
            {
                var key = user.Identity.ToString();
                var throttle = cache[key] as ThrottleContext;

                if (throttle == null)
                {
                    throttle = new ThrottleContext(Limit, _timeout);
                    cache.Insert(key, throttle, null, Cache.NoAbsoluteExpiration, _timeout);
                }

                var success = throttle.Increment();

                response.AppendHeader("X-RateLimit-Limit", Limit.ToString());
                response.AppendHeader("X-RateLimit-Remaining", (Limit - throttle.Count).ToString());
                response.AppendHeader("X-RateLimit-Timeout", _timeout.ToString());

                if (!success)
                    HandleRateLimitedRequest(filterContext);
            }

            base.OnActionExecuting(filterContext);
        }
Beispiel #3
0
        /// <inheritdoc />
        public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var httpContext = context.HttpContext;

            var strategy = await _strategyProvider?.GetThrottleStrategyAsync(httpContext, PolicyName, Routes);

            if (strategy == null)
            {
                return;
            }

            _throttleContext = await _throttleService.EvaluateAsync(httpContext, strategy);

            if (_throttleContext.HasAborted)
            {
                return;
            }

            if (_options.SendThrottleHeaders)
            {
                foreach (var header in _throttleContext.ResponseHeaders.OrderBy(h => h.Key))
                {
                    httpContext.Response.Headers[header.Key] = header.Value;
                }
            }

            if (_throttleContext.HasTooManyRequest)
            {
                string retryAfter = RetryAfterHelper.GetRetryAfterValue(_clock, _options.RetryAfterMode, _throttleContext.RetryAfter);

                // TODO : the <TooManyRequestResult> should wrtite itself the response headers
                context.Result = new TooManyRequestResult(retryAfter);
            }
        }
 public override void AddRateLimitHeaders(ThrottleCounter counter, ThrottleContext throttleContext, AuthenticatedUserRateLimitRequirement requirement)
 {
     throttleContext.ResponseHeaders["X-RateLimit-UserLimit"]     = requirement.MaxValue.ToString(CultureInfo.InvariantCulture);
     throttleContext.ResponseHeaders["X-RateLimit-UserRemaining"] = counter.Remaining(requirement).ToString(CultureInfo.InvariantCulture);
     throttleContext.ResponseHeaders["X -RateLimit-UserReset"]    = counter.Reset.ToEpoch().ToString(CultureInfo.InvariantCulture);
 }
 public override void AddRateLimitHeaders(ThrottleCounter counter, ThrottleContext throttleContext, IPRateLimitRequirement requirement)
 {
     throttleContext.ResponseHeaders["X-RateLimit-IPLimit"]     = requirement.MaxValue.ToString();
     throttleContext.ResponseHeaders["X-RateLimit-IPRemaining"] = counter.Remaining(requirement).ToString();
 }
 public override void AddRateLimitHeaders(ThrottleCounter counter, ThrottleContext throttleContext, ApiKeyRateLimitRequirement requirement)
 {
     throttleContext.ResponseHeaders["X-RateLimit-ClientLimit"]     = requirement.MaxValue.ToString(CultureInfo.InvariantCulture);
     throttleContext.ResponseHeaders["X-RateLimit-ClientRemaining"] = counter.Remaining(requirement).ToString(CultureInfo.InvariantCulture);
 }