https://github.com/moritzrinow/aspnetgeo
Add geo information to the ASP.NET Core request pipeline
https://github.com/moritzrinow/aspnetgeo
asp-net-core geoip geolocation maxmind
Last synced: 3 months ago
JSON representation
Add geo information to the ASP.NET Core request pipeline
- Host: GitHub
- URL: https://github.com/moritzrinow/aspnetgeo
- Owner: moritzrinow
- License: apache-2.0
- Created: 2020-03-17T08:43:14.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-06-18T12:15:16.000Z (about 6 years ago)
- Last Synced: 2025-03-12T05:41:25.329Z (over 1 year ago)
- Topics: asp-net-core, geoip, geolocation, maxmind
- Language: C#
- Size: 27.3 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# aspnetgeo
Add geo information to the ASP.NET Core request pipeline
## Supported providers
- MaxMind
## Usage
#### Install package
dotnet add package AspNetGeo
#### Download free MaxMind databases and group them in a folder
https://dev.maxmind.com/geoip/geoip2/geolite2/
#### Add GeoIpMiddleware to the pipeline
```csharp
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// ...
app.UseGeoIp();
// ...
}
```
#### Use MaxMind as GeoIpProvider
```csharp
public void ConfigureServices(IServiceCollection services)
{
// ...
// Use path to directory with MaxMind database files
services.UseMaxMindGeoIp(Path.Combine(Directory.GetCurrentDirectory(), "data"));
// ...
}
```
#### Include in-memory cache
```csharp
public void ConfigureServices(IServiceCollection services)
{
// ...
// Use path to directory with MaxMind database files
services.UseMaxMindGeoIp(Path.Combine(Directory.GetCurrentDirectory(), "data"));
services.AddGeoIpInMemoryCache();
// ...
}
```
#### Access GeoIp data via HttpContext extension method (data sits in HttpContext.Items under key 'geoIp')
```csharp
IGeoIp geoIP = context.GetGeoIp();
```
#### The GeoIp data structure
The name dictionaries are designed to be: locale ("en", "de" etc.) -> name.
The continent & country codes should come as ISO alpha-2.
```csharp
public interface IGeoIp
{
IReadOnlyDictionary CityNames { get; }
string ContinentCode { get; }
IReadOnlyDictionary ContinentNames { get; }
string CountryCode { get; }
IReadOnlyDictionary CountryNames { get; }
GeoLocation Location { get; }
string PostalCode { get; }
string RegionCode { get; }
IReadOnlyDictionary RegionNames { get; }
}
```