public static void UseHostedWhitelist(
            this IApplicationBuilder app,
            string url,
            HttpClient client = null)
        {
            // Shared HTTP client for all requests
            if (client == null)
            {
                // Use IMemoryCache from DI if available, e.g. if configured
                // with services.AddMemoryCache()
                var cache = (MemoryCache)app.ApplicationServices
                            .GetService(typeof(IMemoryCache))
                            ?? new MemoryCache(new MemoryCacheOptions());

                // Default to 5 minutes caching if not forbidden by NoStore
                // or specified by Cache-Control headers.
                var handler = new CachingHttpHandler(
                    new HttpClientHandler(), cache);
                handler.DefaultCacheDuration = new TimeSpan(0, 5, 0);

                client = new HttpClient(handler);
            }

            // GetStringAsync is called on every IP check, but HttpClient
            // will honour caching headers
            app.UseWhitelist(async() =>
                             Whitelist.Parse(await client.GetStringAsync(url)));
        }
Ejemplo n.º 2
0
        void beginRequest(object sender, EventArgs e)
        {
            var application = (HttpApplication)sender;

            if (_whitelist == null)
            {
                // Have to do this here becase MapPath() isn't allowed in
                // Init().
                string path = application.Server.MapPath(
                    ConfigurationManager.AppSettings["whitelist:Path"]);
                string data = File.ReadAllText(path);

                _whitelist = Whitelist.Parse(data);
            }

            var request = application.Context.Request;
            var ip      = IPAddress.Parse(request.UserHostAddress);

            if (!_whitelist.Match(ip))
            {
                var response = application.Context.Response;
                response.StatusCode = 403;
                response.Output.WriteLine($"Host {ip} is not whitelisted " +
                                          $"for this site.");
                response.End();
            }
        }
        public static void UseStaticWhitelist(
            this IApplicationBuilder app,
            string path)
        {
            // Shared for all requests
            var whitelist = Whitelist.Parse(File.ReadAllText(path));

            app.UseWhitelist(() => Task.FromResult <IWhitelist>(whitelist));
        }
Ejemplo n.º 4
0
        async Task beginRequest(object sender, EventArgs e)
        {
            var application = (HttpApplication)sender;
            var request     = application.Context.Request;
            var ip          = IPAddress.Parse(request.UserHostAddress);

            var data = await _client.GetStringAsync(_url);

            var whitelist = Whitelist.Parse(data);

            if (!whitelist.Match(ip))
            {
                var response = application.Context.Response;
                response.StatusCode = 403;
                response.Output.WriteLine($"Host {ip} is not whitelisted " +
                                          $"for this site.");
                response.End();
            }
            ;
        }