Ejemplo n.º 1
0
        public async Task InvokeAsync(HttpContext context, IRequestRateLimiter requestRateLimiter, RequestRateLimiterSettings settings)
        {
            if (!context.Request.Headers.ContainsKey(settings.ApiKeyHeader))
            {
                throw new Exception();
            }

            var apiKey = context.Request.Headers[settings.ApiKeyHeader];

            if (!string.IsNullOrWhiteSpace(apiKey))
            {
                if (!requestRateLimiter.CanPerformRequest(apiKey))
                {
                    context.Response.StatusCode = 429;
                    await context.Response.WriteAsync("Too many requests");
                }
            }

            await _next(context);
        }