Beispiel #1
0
        public async Task Invoke(HttpContext context, IApiAccessProvider provider)
        {
            var request  = context.Request;
            var headers  = request.Headers;
            var env      = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            var isDevEnv = env == "Local" || env == "Development";

            if (request.Path.ToString().IndexOf("/swagger") == 0)
            {
                await next.Invoke(context);
            }
            else
            {
                var key   = !string.IsNullOrEmpty(headers["X-API-KEY"].ToString()) ? headers["X-API-KEY"].ToString() : "";
                var found = string.IsNullOrEmpty(key) ? false : await validate(key, provider);

                if (found == false)
                {
                    logFailure(context);
                    context.Response.StatusCode = 401;
                    await context.Response.WriteAsync("Invalid API Key");
                }
                else
                {
                    await next.Invoke(context);
                }
            }
        }
Beispiel #2
0
        private async Task <bool> validate(string key, IApiAccessProvider provider)
        {
            var found = await provider.getByKey(key);

            return(found != null);
        }