Skip to content

IP-Block-Ltd/ipblock-aspnet

Repository files navigation

⚠️ 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).

IP Block — ASP.NET Core Middleware

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.

Install

dotnet add package IpBlock.AspNetCore

Register

In 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();

Configure (appsettings.json)

{
  "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" ]
  }
}

How it works

  • Builds {api_key, site_id, ip, user_agent, referrer} and POSTs it with a typed HttpClient and a 1 second (TimeoutMs) cancellation.
  • Blocks only when the response is {"action":"block"}.
  • Fails open on any error/timeout/non-2xx/missing action (FailOpen=false to fail closed).
  • Caches each decision for CacheTtl seconds in IMemoryCache, keyed by md5(ip|user_agent|referrer).
  • Honours Whitelist (individual IPs and CIDR ranges).
  • Reads the real client IP; with BehindProxy=true it trusts CF-Connecting-IP then the first X-Forwarded-For hop.

About

IP Block protection for ASP.NET Core — ip-block.com integration. Untested at the moment; please feel free to modify. GPLv3.

Topics

Resources

License

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages