This distribution provides an API for the GeoIP2 and GeoLite2 web services and databases.
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 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.
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.
To use the web service API with HttpClient factory pattern as a Typed client you need to do the following:
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>();
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"
},
...
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;
}
}
// 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"]); // '美国'
}
// 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"]); // '美国'
}
// 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
}
// 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
}
// 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
}
// 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
}
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.
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'
}
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'
}
// 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
}
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'
}
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'
}
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
}
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