⚠️ Status: untested. This extension is provided as-is and has not been tested in production. Please feel free to fork, modify, improve, and open pull requests.Licensed under GNU GPLv3 (see LICENSE).
A class library providing middleware, an IApplicationBuilder extension, and
strongly-typed options that check each request against the
ip-block.com service.
Targets: ASP.NET Core on .NET 6.0 and .NET 8.0.
dotnet add package IpBlock.AspNetCoreIn Program.cs (minimal hosting):
using IpBlock.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
// Bind from the "IpBlock" section of appsettings.json...
builder.Services.AddIpBlock(builder.Configuration);
// ...or configure inline:
// builder.Services.AddIpBlock(o =>
// {
// o.SiteId = "your-site-id";
// o.ApiKey = "your-api-key";
// o.BehindProxy = true;
// });
var app = builder.Build();
app.UseIpBlock(); // add early, before UseRouting/endpoints
app.MapGet("/", () => "Hello");
app.Run();{
"IpBlock": {
"Enabled": true,
"SiteId": "your-site-id",
"ApiKey": "your-api-key",
"ApiUrl": "https://api.ip-block.com/v1/check",
"FailOpen": true,
"CacheTtl": 300,
"TimeoutMs": 1000,
"BehindProxy": false,
"BlockAction": "403",
"RedirectUrl": "https://www.ip-block.com/blocked.php",
"BlockMessage": "Access denied.",
"Whitelist": [ "127.0.0.1", "10.0.0.0/8" ]
}
}- Builds
{api_key, site_id, ip, user_agent, referrer}andPOSTs it with a typedHttpClientand a 1 second (TimeoutMs) cancellation. - Blocks only when the response is
{"action":"block"}. - Fails open on any error/timeout/non-2xx/missing
action(FailOpen=falseto fail closed). - Caches each decision for
CacheTtlseconds inIMemoryCache, keyed bymd5(ip|user_agent|referrer). - Honours
Whitelist(individual IPs and CIDR ranges). - Reads the real client IP; with
BehindProxy=trueit trustsCF-Connecting-IPthen the firstX-Forwarded-Forhop.