A midleware that allows whitelist or blacklist (Ip filtering) incoming requests.
It supports:
- single IP
- IP range IPv4 and IPv6
- wildcard (*)
- configurable caching
Configuration of whitelist and blacklist addresses can be made by: asp.net Core configuration system or by implementing custom IIpRulesProvider
.
Install-Package AspNetCore.IpFiltering
public class Startup
{
// ....
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddIpFiltering(_configuration.GetSection("IpFilteringConfiguration"));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// ...
app.UseIpFilteringMiddleware();
app.UseMvc();
}
}
{
"IpFilteringConfiguration" : {
"Whitelist": ["*"],
"Blacklist": [""],
"IpRulesSource": "Configuration",
"IpRulesCacheSource" : "Configuration",
"DefaultIpRuleCacheDuration" : "300",
"FailureHttpStatusCode": "403",
"FailureMessage" : "IP address rejected"
}
}
public class Startup
{
// ....
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddTransient<IIpRulesProvider, InMemoryListRulesProvider>();
services.AddIpFiltering(new WhitelistOptions
{
IpListSource = IpListSource.Provider,
FailureHttpStatusCode = 404
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// ...
app.UseIpFilteringMiddleware();
app.UseMvc();
}
}
public class InMemoryListRulesProvider : IIpRulesProvider
{
public Task<IpRule[]> GetIpRules()
{
return Task.FromResult(new List<IpRule>()
{
// blacklist
new IpRule(IpAddressRangeWithWildcard.Parse("127.0.0.4"),
IpRuleType.Blacklist),
new IpRule(IpAddressRangeWithWildcard.Parse("127.0.0.4"),
IpRuleType.Blacklist),
// whitelist
new IpRule(IpAddressRangeWithWildcard.GetWildcardRange(),IpRuleType.Whitelist)
}.ToArray());
}
}
In real implementation you would make a db call instead of returning static list.
AspnetCore.IpFiltering takes Client IP address from context.Connection.RemoteIpAddress
. It will work in case of exposing your application without any reverse proxy. If you want to make it work with reverse proxy, please use ForwardedHeaderMiddleware before IpFilteringMiddleware
.
Library allows to specify path regex patterns, that will be excluded from white list checking. To set ignored paths, you can use IgnoredPaths
parameter on configuration.
"IpFilteringConfiguration" : {
// some other configuration parameters
"IgnoredPaths": [
"\\/api\\/some-data\\/[\\d]{1,}",
"api\\/some-unguarded-data"]
},
If you want to track unknown IP addresses without blocking them, you need to turn LearningMode on.
https://github.com/garfieldos/AspNetCore.IpFiltering/tree/master/src/samples