MaxMind.GeoIP2 5.2.0

Prefix Reserved
dotnet add package MaxMind.GeoIP2 --version 5.2.0                
NuGet\Install-Package MaxMind.GeoIP2 -Version 5.2.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MaxMind.GeoIP2" Version="5.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MaxMind.GeoIP2 --version 5.2.0                
#r "nuget: MaxMind.GeoIP2, 5.2.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MaxMind.GeoIP2 as a Cake Addin
#addin nuget:?package=MaxMind.GeoIP2&version=5.2.0

// Install MaxMind.GeoIP2 as a Cake Tool
#tool nuget:?package=MaxMind.GeoIP2&version=5.2.0                

GeoIP2 .NET API

NuGet API Docs

Description

This distribution provides an API for the GeoIP2 and GeoLite2 web services and databases.

Installation

NuGet

We recommend installing this library with NuGet. To do this, type the following into the Visual Studio Package Manager Console:

install-package MaxMind.GeoIP2

IP Geolocation Usage

IP geolocation is inherently imprecise. Locations are often near the center of the population. Any location provided by a GeoIP2 database or web service should not be used to identify a particular address or household.

Web Service Usage

To use the web service API, first create a new WebServiceClient object with your account ID and license key:

var client = new WebServiceClient(42, "license_key1");

To query the GeoLite2 web service, you must set the host to geolite.info:

var client = new WebServiceClient(42, "license_key1", host: "geolite.info");

To query the Sandbox GeoIP2 web service, you must set the host to sandbox.maxmind.com:

var client = new WebServiceClient(42, "license_key1", host: "sandbox.maxmind.com");

You may also specify the fall-back locales, the host, or the timeout as optional parameters. See the API docs for more information.

This object is safe to share across threads. If you are making multiple requests, the object should be reused to so that new connections are not created for each request. Once you have finished making requests, you should dispose of the object to ensure the connections are closed and any resources are promptly returned to the system.

You may then call the sync or async method corresponding to the specific end point, passing it the IP address you want to look up or no parameters if you want to look up the current device.

If the request succeeds, the method call will return a response class for the endpoint you called. This response in turn contains multiple model classes, each of which represents part of the data returned by the web service.

See the API documentation for more details.

ASP.NET Core Usage

To use the web service API with HttpClient factory pattern as a Typed client you need to do the following:

  1. Add the following lines to Startup.cs ConfigureServices method:
// Configure to read configuration options from MaxMind section
services.Configure<WebServiceClientOptions>(Configuration.GetSection("MaxMind"));

// Configure dependency injection for WebServiceClient
services.AddHttpClient<WebServiceClient>();
  1. Add configuration in your appsettings.json with your account ID and license key.
...
  "MaxMind": {
    "AccountId": 123456,
    "LicenseKey": "1234567890",

    // Optionally set a timeout. The default is 3000 ms.
    // "Timeout": 3000,

    // Optionally set host. "geolite.info" will use the GeoLite2
    // web service instead of GeoIP2. "sandbox.maxmind.com" will use the
    // Sandbox GeoIP2 web service instead of the production GeoIP2 web
    // service.
    //
    // "Host": "geolite.info"
  },
...
  1. Inject the WebServiceClient where you need to make the call and use it.
[ApiController]
[Route("[controller]")]
public class MaxMindController : ControllerBase
{
    private readonly WebServiceClient _maxMindClient;

    public MaxMindController(WebServiceClient maxMindClient)
    {
        _maxMindClient = maxMindClient;
    }

    [HttpGet]
    public async Task<string> Get()
    {
        var location = await _maxMindClient.CountryAsync();

        return location.Country.Name;
    }
}

Web Service Example

Country Service (Sync)

// If you are making multiple requests, a single WebServiceClient
// should be shared across requests to allow connection reuse. The
// class is thread safe.
//
// Replace "42" with your account ID and "license_key" with your license
// key. Set the named host argument to "geolite.info" to use the GeoLite2
// web service instead of GeoIP2. Set the named host argument to
// "sandbox.maxmind.com" to use the Sandbox GeoIP2 web service instead of
// the production GeoIP2 web service.
using (var client = new WebServiceClient(42, "license_key"))
{
    // Do the lookup
    var response = client.Country("128.101.101.101");

    Console.WriteLine(response.Country.IsoCode);        // 'US'
    Console.WriteLine(response.Country.Name);           // 'United States'
    Console.WriteLine(response.Country.Names["zh-CN"]); // '美国'
}

Country Service (Async)

// If you are making multiple requests, a single WebServiceClient
// should be shared across requests to allow connection reuse. The
// class is thread safe.
//
// Replace "42" with your account ID and "license_key" with your license
// key. Set the named host argument to "geolite.info" to use the GeoLite2
// web service instead of GeoIP2. Set the named host argument to
// "sandbox.maxmind.com" to use the Sandbox GeoIP2 web service instead of
// the production GeoIP2 web service.
using (var client = new WebServiceClient(42, "license_key"))
{
    // Do the lookup
    var response = await client.CountryAsync("128.101.101.101");

    Console.WriteLine(response.Country.IsoCode);        // 'US'
    Console.WriteLine(response.Country.Name);           // 'United States'
    Console.WriteLine(response.Country.Names["zh-CN"]); // '美国'
}

City Plus Service (Sync)

// If you are making multiple requests, a single WebServiceClient
// should be shared across requests to allow connection reuse. The
// class is thread safe.
//
// Replace "42" with your account ID and "license_key" with your license
// key. Set the named host argument to "geolite.info" to use the GeoLite2
// web service instead of GeoIP2. Set the named host argument to
// "sandbox.maxmind.com" to use the Sandbox GeoIP2 web service instead of
// the production GeoIP2 web service.
using (var client = new WebServiceClient(42, "license_key"))
{
    // Do the lookup
    var response = client.City("128.101.101.101");

    Console.WriteLine(response.Country.IsoCode);        // 'US'
    Console.WriteLine(response.Country.Name);           // 'United States'
    Console.WriteLine(response.Country.Names["zh-CN"]); // '美国'

    Console.WriteLine(response.MostSpecificSubdivision.Name);    // 'Minnesota'
    Console.WriteLine(response.MostSpecificSubdivision.IsoCode); // 'MN'

    Console.WriteLine(response.City.Name); // 'Minneapolis'

    Console.WriteLine(response.Postal.Code); // '55455'

    Console.WriteLine(response.Location.Latitude);  // 44.9733
    Console.WriteLine(response.Location.Longitude); // -93.2323
}

City Plus Service (Async)

// If you are making multiple requests, a single WebServiceClient
// should be shared across requests to allow connection reuse. The
// class is thread safe.
//
// Replace "42" with your account ID and "license_key" with your license
// key. Set the named host argument to "geolite.info" to use the GeoLite2
// web service instead of GeoIP2. Set the named host argument to
// "sandbox.maxmind.com" to use the Sandbox GeoIP2 web service instead of
// the production GeoIP2 web service.
using (var client = new WebServiceClient(42, "license_key"))
{
    // Do the lookup
    var response = await client.CityAsync("128.101.101.101");

    Console.WriteLine(response.Country.IsoCode);        // 'US'
    Console.WriteLine(response.Country.Name);           // 'United States'
    Console.WriteLine(response.Country.Names["zh-CN"]); // '美国'

    Console.WriteLine(response.MostSpecificSubdivision.Name);    // 'Minnesota'
    Console.WriteLine(response.MostSpecificSubdivision.IsoCode); // 'MN'

    Console.WriteLine(response.City.Name); // 'Minneapolis'

    Console.WriteLine(response.Postal.Code); // '55455'

    Console.WriteLine(response.Location.Latitude);  // 44.9733
    Console.WriteLine(response.Location.Longitude); // -93.2323
}

Insights Service (Sync)

// If you are making multiple requests, a single WebServiceClient
// should be shared across requests to allow connection reuse. The
// class is thread safe.
//
// Replace "42" with your account ID and "license_key" with your license
// key. The GeoLite2 web service does not support Insights. Set the named
// host argument to "sandbox.maxmind.com" to use the Sandbox GeoIP2 web
// service instead of the production GeoIP2 web service.
using (var client = new WebServiceClient(42, "license_key"))
{
    // Do the lookup
    var response = client.Insights("128.101.101.101");

    Console.WriteLine(response.Country.IsoCode);        // 'US'
    Console.WriteLine(response.Country.Name);           // 'United States'
    Console.WriteLine(response.Country.Names["zh-CN"]); // '美国'

    Console.WriteLine(response.MostSpecificSubdivision.Name);    // 'Minnesota'
    Console.WriteLine(response.MostSpecificSubdivision.IsoCode); // 'MN'

    Console.WriteLine(response.City.Name); // 'Minneapolis'

    Console.WriteLine(response.Postal.Code); // '55455'

    Console.WriteLine(response.Location.Latitude);  // 44.9733
    Console.WriteLine(response.Location.Longitude); // -93.2323
}

Insights Service (Async)

// If you are making multiple requests, a single WebServiceClient
// should be shared across requests to allow connection reuse. The
// class is thread safe.
//
// Replace "42" with your account ID and "license_key" with your license
// key. The GeoLite2 web service does not support Insights. Set the named
// host argument to "sandbox.maxmind.com" to use the Sandbox GeoIP2 web
// service instead of the production GeoIP2 web service.
using (var client = new WebServiceClient(42, "license_key"))
{
    // Do the lookup
    var response = await client.InsightsAsync("128.101.101.101");

    Console.WriteLine(response.Country.IsoCode);        // 'US'
    Console.WriteLine(response.Country.Name);           // 'United States'
    Console.WriteLine(response.Country.Names["zh-CN"]); // '美国'

    Console.WriteLine(response.MostSpecificSubdivision.Name);    // 'Minnesota'
    Console.WriteLine(response.MostSpecificSubdivision.IsoCode); // 'MN'

    Console.WriteLine(response.City.Name); // 'Minneapolis'

    Console.WriteLine(response.Postal.Code); // '55455'

    Console.WriteLine(response.Location.Latitude);  // 44.9733
    Console.WriteLine(response.Location.Longitude); // -93.2323
}

Database Usage

To use the database API, you must create a new DatabaseReader with a string representation of the path to your GeoIP2 database. You may also specify the file access mode. You may then call the appropriate method (e.g., city) for your database, passing it the IP address you want to look up.

If the lookup succeeds, the method call will return a response class for the GeoIP2 lookup. This class in turn contains multiple model classes, each of which represents part of the data returned by the database.

We recommend reusing the DatabaseReader object rather than creating a new one for each lookup. The creation of this object is relatively expensive as it must read in metadata for the file.

See the API documentation for more details.

Database Examples

Anonymous IP Database


using (var reader = new DatabaseReader("GeoIP2-Anonymous-IP.mmdb"))
{
    var response = reader.AnonymousIP("85.25.43.84");
    Console.WriteLine(response.IsAnonymous);        // true
    Console.WriteLine(response.IsAnonymousVpn);     // false
    Console.WriteLine(response.IsHostingProvider);  // false
    Console.WriteLine(response.IsPublicProxy);      // false
    Console.WriteLine(response.IsResidentialProxy); // false
    Console.WriteLine(response.IsTorExitNode);      // true
    Console.WriteLine(response.IPAddress);          // '85.25.43.84'
}

ASN


using (var reader = new DatabaseReader("GeoLite2-ASN.mmdb"))
{
    var response = reader.Asn("85.25.43.84");
    Console.WriteLine(response.AutonomousSystemNumber); // 217
    Console.WriteLine(response.AutonomousSystemOrganization); // 'University of Minnesota'
    Console.WriteLine(response.IPAddress); // '128.101.101.101'
}

City Database

// This creates the DatabaseReader object, which should be reused across
// lookups.
using (var reader = new DatabaseReader("GeoIP2-City.mmdb"))
{
    // Replace "City" with the appropriate method for your database, e.g.,
    // "Country".
    var city = reader.City("128.101.101.101");

    Console.WriteLine(city.Country.IsoCode); // 'US'
    Console.WriteLine(city.Country.Name); // 'United States'
    Console.WriteLine(city.Country.Names["zh-CN"]); // '美国'

    Console.WriteLine(city.MostSpecificSubdivision.Name); // 'Minnesota'
    Console.WriteLine(city.MostSpecificSubdivision.IsoCode); // 'MN'

    Console.WriteLine(city.City.Name); // 'Minneapolis'

    Console.WriteLine(city.Postal.Code); // '55455'

    Console.WriteLine(city.Location.Latitude); // 44.9733
    Console.WriteLine(city.Location.Longitude); // -93.2323
}

Connection-Type Database


using (var reader = new DatabaseReader("GeoIP2-Connection-Type.mmdb"))
{
    var response = reader.ConnectionType("128.101.101.101");
    Console.WriteLine(response.ConnectionType); // 'Corporate'
    Console.WriteLine(response.IPAddress); // '128.101.101.101'
}

Domain Database


using (var reader = new DatabaseReader("GeoIP2-Domain.mmdb"))
{
    var response = reader.Domain("128.101.101.101");
    Console.WriteLine(response.Domain); // 'umn.edu'
    Console.WriteLine(response.IPAddress); // '128.101.101.101'
}

Enterprise Database

using (var reader = new DatabaseReader("/path/to/GeoIP2-Enterprise.mmdb"))
{
    //  Use the Enterprise(ip) method to do a lookup in the Enterprise database
    var response = reader.enterprise("128.101.101.101");

    var country = response.Country;
    Console.WriteLine(country.IsoCode);            // 'US'
    Console.WriteLine(country.Name);               // 'United States'
    Console.WriteLine(country.Names["zh-CN"]);     // '美国'
    Console.WriteLine(country.Confidence);         // 99

    var subdivision = response.MostSpecificSubdivision;
    Console.WriteLine(subdivision.Name);           // 'Minnesota'
    Console.WriteLine(subdivision.IsoCode);        // 'MN'
    Console.WriteLine(subdivision.Confidence);     // 77

    var city = response.City;
    Console.WriteLine(city.Name);       // 'Minneapolis'
    Console.WriteLine(city.Confidence); // 11

    var postal = response.Postal;
    Console.WriteLine(postal.Code); // '55455'
    Console.WriteLine(postal.Confidence); // 5

    var location = response.Location;
    Console.WriteLine(location.Latitude);       // 44.9733
    Console.WriteLine(location.Longitude);      // -93.2323
    Console.WriteLine(location.AccuracyRadius); // 50
}

ISP Database


using (var reader = new DatabaseReader("GeoIP2-ISP.mmdb"))
{
    var response = reader.Isp("128.101.101.101");
    Console.WriteLine(response.AutonomousSystemNumber); // 217
    Console.WriteLine(response.AutonomousSystemOrganization); // 'University of Minnesota'
    Console.WriteLine(response.Isp); // 'University of Minnesota'
    Console.WriteLine(response.Organization); // 'University of Minnesota'
    Console.WriteLine(response.IPAddress); // '128.101.101.101'
}

Exceptions

Database

If the database is corrupt or otherwise invalid, a MaxMind.Db.InvalidDatabaseException will be thrown.

If an address is not available in the database, a AddressNotFoundException will be thrown.

Web Service

For details on the possible errors returned by the web service itself, see the GeoIP2 web service documentation.

If the web service returns an explicit error document, this is thrown as a AddressNotFoundException, a AuthenticationException, a InvalidRequestException, or a OutOfQueriesException.

If some sort of transport error occurs, an HttpException is thrown. This is thrown when some sort of unanticipated error occurs, such as the web service returning a 500 or an invalid error document. If the web service request returns any status code besides 200, 4xx, or 5xx, this also becomes a HttpException.

Finally, if the web service returns a 200 but the body is invalid, the client throws a GeoIP2Exception. This exception also is the parent exception to the above exceptions.

Values to use for Database or Dictionary Keys

We strongly discourage you from using a value from any Names property as a key in a database or dictionary.

These names may change between releases. Instead we recommend using one of the following:

  • MaxMind.GeoIP2.Model.City - City.GeoNameId
  • MaxMind.GeoIP2.Model.Continent - Continent.Code or Continent.GeoNameId
  • MaxMind.GeoIP2.Model.Country and MaxMind.GeoIP2.Model.RepresentedCountry
    • Country.IsoCode or Country.GeoNameId
  • MaxMind.GeoIP2.Model.Subdivision - Subdivision.IsoCode or Subdivision.GeoNameId

Multi-Threaded Use

This API fully supports use in multi-threaded applications. When using the DatabaseReader in a multi-threaded application, we suggest creating one object and sharing that among threads.

What data is returned?

While many of the end points return the same basic records, the attributes which can be populated vary between end points. In addition, while an end point may offer a particular piece of data, MaxMind does not always have every piece of data for any given IP address.

Because of these factors, it is possible for any end point to return a record where some or all of the attributes are unpopulated.

See the GeoIP2 web services documentations for details on what data each end point may return.

The only piece of data which is always returned is the ipAddress attribute in the MaxMind.GeoIP2.Traits record.

Integration with GeoNames

GeoNames offers web services and downloadable databases with data on geographical features around the world, including populated places. They offer both free and paid premium data. Each feature is unique identified by a geonameId, which is an integer.

Many of the records returned by the GeoIP2 web services and databases include a geonameId property. This is the ID of a geographical feature (city, region, country, etc.) in the GeoNames database.

Some of the data that MaxMind provides is also sourced from GeoNames. We source things like place names, ISO codes, and other similar data from the GeoNames premium data set.

Reporting data problems

If the problem you find is that an IP address is incorrectly mapped, please submit your correction to MaxMind.

If you find some other sort of mistake, like an incorrect spelling, please check the GeoNames site first. Once you've searched for a place and found it on the GeoNames map view, there are a number of links you can use to correct data ("move", "edit", "alternate names", etc.). Once the correction is part of the GeoNames data set, it will be automatically incorporated into future MaxMind releases.

If you are a paying MaxMind customer and you're not sure where to submit a correction, please contact MaxMind support for help.

Other Support

Please report all issues with this code using the GitHub issue tracker.

If you are having an issue with a MaxMind service that is not specific to the client API, please see our support page.

Contributing

Patches and pull requests are encouraged. Please include unit tests whenever possible.

Versioning

The API uses Semantic Versioning. However, adding new methods to IGeoIP2Provider, IGeoIP2DatabaseReader, and IGeoIP2WebServicesClient will not be considered a breaking change. These interfaces only exist to facilitate unit testing and dependency injection. They will be updated to match additional methods added to the DatabaseReader and WebServiceClient. Such additions will be accompanied by a minor version bump (e.g., 1.2.x to 1.3.0).

This software is Copyright (c) 2013-2023 by MaxMind, Inc.

This is free software, licensed under the Apache License, Version 2.0.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (39)

Showing the top 5 NuGet packages that depend on MaxMind.GeoIP2:

Package Downloads
Kentico.Xperience.Libraries

The runtime assemblies for libraries and applications that use Kentico Xperience API.

Kentico.Libraries

The runtime assemblies for libraries and applications that use Kentico API.

MaxMind.MinFraud

API for MaxMind minFraud Score and Insights web services

Firewall

An ASP.NET Core middleware for request filtering.

Walter.Web.FireWall.Geo.MaxMind

Use MaxMind Geo data service or API keys to make the Walter.Web.IFireWall interface geographically aware allowing you to block requests for certain countries and enables you to render a different result based on the region or country the request came from. The Package adds functionality to NuGet package Walter.Web.Firewall services.AddFireWall(FireWallTrial.License, FireWallTrial.DomainKey , domainName: new Uri("https://www.your-domain.com", UriKind.Absolute) , options => { //your firewall settings }).UseGeography(directory:new DirectoryInfo("d:\\MaxMind")); This assumes that data files are in d:\MaxMind folder leave blank if you use App_Data and in your application configuration you can set blocking scope like this app.UseFireWall() .UseGeoBlockingMiddleware(options => { options.Block(new[]{GeoLocation.China ,GeoLocation.RussianFederation}); }); More information on how to use this Add-On is available in this manual https://www.asp-waf.com/download/ASP-WAF-FireWall-Getting-Started.pdf

GitHub repositories (23)

Showing the top 5 popular GitHub repositories that depend on MaxMind.GeoIP2:

Repository Stars
netchx/netch
A simple proxy client
nopSolutions/nopCommerce
ASP.NET Core eCommerce software. nopCommerce is a free and open-source shopping cart.
TechnitiumSoftware/DnsServer
Technitium DNS Server
smartstore/SmartStoreNET
Open Source ASP.NET MVC Enterprise eCommerce Shopping Cart Solution
exceptionless/Exceptionless
Exceptionless application
Version Downloads Last updated
5.2.0 1,364,097 12/5/2023
5.1.0 2,801,045 2/4/2022
5.0.0 4,613 2/4/2022
4.1.0 494,539 11/19/2021
4.0.1 1,273,324 11/19/2020
4.0.0 12,405 11/17/2020
3.3.0 314,241 9/25/2020
3.2.0 1,967,999 4/28/2020
3.1.0 766,339 12/6/2019
3.0.0 3,092,940 4/11/2018
2.10.0 800,504 1/19/2018
2.9.0 849,972 10/27/2017
2.8.0 208,491 5/8/2017
2.7.2 623,867 11/22/2016
2.7.1 188,547 8/8/2016
2.7.0 5,214 8/1/2016
2.7.0-beta2 2,935 6/2/2016
2.7.0-beta1 2,211 5/15/2016
2.6.0 317,165 4/15/2016
2.6.0-beta3 6,167 2/10/2016
2.6.0-beta2 2,332 1/29/2016
2.6.0-beta1 2,186 1/18/2016
2.5.0 59,216 12/4/2015
2.4.0 96,038 9/23/2015
2.4.0-beta1 2,280 9/10/2015
2.3.1 55,520 7/21/2015
2.3.1-beta1 2,319 6/30/2015
2.3.0 136,748 6/29/2015
2.2.0 71,884 5/19/2015
2.1.0 174,773 11/6/2014
2.0.0 87,132 9/29/2014
0.5.0 4,065 9/24/2014
0.4.0 24,455 7/22/2014
0.3.3 17,110 6/2/2014
0.3.2 23,627 4/9/2014
0.3.1 36,084 2/13/2014
0.3.0 12,256 11/15/2013
0.2.0 3,018 10/25/2013
0.1.1 3,599 10/4/2013
0.1.0 5,935 9/20/2013