CountryLookup

Returns a country code based on an IP address.

How to use

Step 1: Configure MaxMind license key

<sharpNick>
	<countryLookup maxMindLicenseKey="abcdefg01234" />
</sharpNick>

See configuration reference to use the <sharpNick> node. To get a MaxMind license key, sign up a site license with update subscriptions.

Step 2: Set read/write permissions on App_Data

Check this MSDN article to find out how to do that, if you've not done that already.

Step 3: Use!

var country = CountryLookup.GetCountryCode("164.71.1.146");
Console.WriteLine(country); // US

Automatic updating

The MaxMind geoip database will go out of date within weeks. MaxMind provides a new database about every week. This class automatically downloads and replaces the old database file at 7am (computer time zone) every day.

This class downloads the database file on first use, which blocks off the calling thread (and any other new calling threads) until the file is downloaded and made available.

Location of database file

On a ASP.NET web application, the file will be downloaded to the app's App_Data directory. For non web applications, the file will be downloaded to the executing directory.

Tips

Use GeographyWizard to get the name of the country returned:

var country = CountryLookup.GetCountryCode("164.71.1.146");
var name = GeographyWizard.GetCountryName(country);
Console.WriteLine(name); // United States

Credits

The code that queries the database file is based on Ting Huang's Java port, and is listed on MaxMind.

Last edited Oct 23, 2010 at 3:51 AM by weiwong, version 14

Comments

No comments yet.