mostlylucid.geodetection
6.7.5
dotnet add package mostlylucid.geodetection --version 6.7.5
NuGet\Install-Package mostlylucid.geodetection -Version 6.7.5
<PackageReference Include="mostlylucid.geodetection" Version="6.7.5" />
<PackageVersion Include="mostlylucid.geodetection" Version="6.7.5" />
<PackageReference Include="mostlylucid.geodetection" />
paket add mostlylucid.geodetection --version 6.7.5
#r "nuget: mostlylucid.geodetection, 6.7.5"
#:package mostlylucid.geodetection@6.7.5
#addin nuget:?package=mostlylucid.geodetection&version=6.7.5
#tool nuget:?package=mostlylucid.geodetection&version=6.7.5
Mostlylucid.GeoDetection
Geographic location detection and routing middleware for ASP.NET Core applications with multiple provider support.
Features
- Multiple Providers: ip-api.com (free, no account), MaxMind GeoLite2 (local database), or simple mock
- Country-Based Routing: Allow/block by country code with middleware and attributes
- Memory Caching: Fast in-memory caching enabled by default
- Optional Database Caching: SQLite/EF Core persistent cache (opt-in)
- Auto-Updates: Automatic MaxMind database downloads and updates
Installation
dotnet add package Mostlylucid.GeoDetection
Quick Start
Option 1: ip-api.com (Easiest - No Setup Required)
// Uses free ip-api.com API, no account needed
builder.Services.AddGeoRoutingWithIpApi();
app.UseGeoRouting();
Option 2: MaxMind GeoLite2 (Best for Production)
builder.Services.AddGeoRouting(
configureProvider: options =>
{
options.Provider = GeoProvider.MaxMindLocal;
options.AccountId = 123456; // Free account from maxmind.com
options.LicenseKey = "your-license-key";
}
);
app.UseGeoRouting();
Option 3: Simple Mock (Development/Testing)
builder.Services.AddGeoRoutingSimple();
Providers
| Provider | Setup | Pros | Cons |
|---|---|---|---|
| IpApi | None | Free, no account needed | Rate limited (45/min), online only |
| MaxMindLocal | Free account | Fast, offline, accurate | Requires account, ~60MB database |
| Simple | None | No dependencies | Mock data only |
Getting a MaxMind Account (Free)
- Sign up at maxmind.com/en/geolite2/signup
- Generate a license key in your account
- Configure AccountId and LicenseKey
- Database auto-downloads on startup
Configuration
appsettings.json
{
"GeoLite2": {
"Provider": "IpApi",
"AccountId": null,
"LicenseKey": null,
"DatabasePath": "data/GeoLite2-City.mmdb",
"EnableAutoUpdate": true,
"CacheDuration": "01:00:00",
"FallbackToSimple": true
}
}
Provider Options
builder.Services.AddGeoRouting(
configureRouting: options =>
{
options.Enabled = true;
options.AllowedCountries = new[] { "US", "CA", "GB" };
options.BlockedCountries = new[] { "XX" };
options.AddCountryHeader = true; // Adds X-Country header
options.StoreInContext = true; // Store in HttpContext.Items
options.EnableTestMode = true; // Allow header override
},
configureProvider: options =>
{
options.Provider = GeoProvider.IpApi;
options.CacheDuration = TimeSpan.FromHours(1);
}
);
Database Caching (Optional)
By default, only memory caching is used. To persist lookups to a database:
// Add SQLite database cache (call BEFORE AddGeoRouting)
builder.Services.AddGeoCacheDatabase("Data Source=data/geocache.db");
builder.Services.AddGeoRouting(
configureCache: options =>
{
options.Enabled = true;
options.CacheExpiration = TimeSpan.FromDays(30);
}
);
Use any EF Core provider by configuring DbContext yourself:
builder.Services.AddDbContext<GeoDbContext>(options =>
options.UseSqlServer(connectionString));
Country-Based Routing
Endpoint Extensions
app.MapGet("/us-only", () => "US Only Content")
.RequireCountry("US");
app.MapGet("/eu-content", () => "EU Content")
.RequireCountry("DE", "FR", "IT", "ES", "NL", "BE");
app.MapGet("/blocked", () => "Not for XX")
.BlockCountries("XX");
MVC Attributes
[GeoRoute(AllowedCountries = new[] { "US", "CA" })]
public class NorthAmericaController : Controller
{
public IActionResult Index() => View();
}
[GeoRoute(BlockedCountries = new[] { "XX" })]
public IActionResult Restricted() => View();
Access Location Data
public class MyController : Controller
{
private readonly IGeoLocationService _geoService;
public MyController(IGeoLocationService geoService)
{
_geoService = geoService;
}
public async Task<IActionResult> Index()
{
var clientIp = HttpContext.Connection.RemoteIpAddress?.ToString();
var location = await _geoService.GetLocationAsync(clientIp!);
return Ok(new
{
location?.CountryCode,
location?.CountryName,
location?.City,
location?.Latitude,
location?.Longitude,
location?.TimeZone,
location?.IsVpn,
location?.IsHosting
});
}
}
GeoLocation Model
public class GeoLocation
{
public string CountryCode { get; set; } // "US", "GB", etc.
public string CountryName { get; set; } // "United States"
public string? ContinentCode { get; set; } // "NA", "EU", "AS"
public string? RegionCode { get; set; } // State/region
public string? City { get; set; }
public double? Latitude { get; set; }
public double? Longitude { get; set; }
public string? TimeZone { get; set; } // "America/New_York"
public bool IsVpn { get; set; } // Known VPN/proxy
public bool IsHosting { get; set; } // Datacenter IP
}
Statistics
var stats = geoService.GetStatistics();
// stats.TotalLookups, stats.CacheHits, stats.DatabaseLoaded, etc.
Notes
- Supports reverse proxies (X-Forwarded-For, CF-Connecting-IP)
- Country codes follow ISO 3166-1 alpha-2 standard
- ip-api.com is rate limited to 45 requests/minute (free tier)
- MaxMind databases update weekly (auto-update enabled by default)
- Private/reserved IPs return "XX" country code
License
Unlicense - Public Domain
Links
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- MaxMind.GeoIP2 (>= 5.4.1)
- Microsoft.EntityFrameworkCore (>= 10.0.8)
- Microsoft.EntityFrameworkCore.Sqlite (>= 10.0.8)
- Mostlylucid.Common (>= 0.0.0-alpha.0.1005)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on mostlylucid.geodetection:
| Package | Downloads |
|---|---|
|
Mostlylucid.GeoDetection.Contributor
GeoDetection contributor for BotDetection - provides geographic location analysis and geo-based bot detection signals including country/region validation and geo-inconsistency detection. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 6.7.5 | 31 | 5/23/2026 |
| 6.7.5-alpha0 | 34 | 5/23/2026 |
| 6.7.0-alpha1 | 35 | 5/21/2026 |
| 6.7.0-alpha0 | 45 | 5/21/2026 |
| 6.6.0-alpha1 | 47 | 5/18/2026 |
| 6.5.3 | 43 | 5/17/2026 |
| 6.5.2 | 44 | 5/17/2026 |
| 6.5.1-rc3 | 50 | 5/17/2026 |
| 6.5.1-rc2 | 39 | 5/17/2026 |
| 6.5.1-rc1 | 41 | 5/17/2026 |
| 6.5.1-rc0 | 58 | 5/17/2026 |
| 6.5.0-rc1 | 44 | 5/17/2026 |
| 6.5.0-rc0 | 46 | 5/16/2026 |
| 6.4.7-rc3 | 54 | 5/16/2026 |
| 6.4.7-rc2 | 46 | 5/16/2026 |
| 6.4.7-rc1 | 48 | 5/15/2026 |
| 6.4.6 | 51 | 5/15/2026 |
| 6.4.5-rc3 | 40 | 5/17/2026 |
| 6.4.5-rc2 | 44 | 5/17/2026 |
| 4.7.6-rc0 | 52 | 5/15/2026 |
1.0.0
Initial release of Mostlylucid.GeoDetection.
Features:
- IP-based geolocation detection
- Country-based routing middleware
- Region and city detection support
- Endpoint routing with GeoRoute attribute
- Configurable allowed/blocked country lists
- Memory caching for performance
- ASP.NET Core middleware integration
- Dependency injection support via IServiceCollection
- Configurable options via GeoRoutingOptions