Local lightning fast IP Address to Country lookups via Sql Server
-
Download the dbip-country.csv file from https://db-ip.com/db/#downloads (it's free) This contains the mapping between the IP Address blocks and their countries.
-
Add the file to Your Azure Storage account. You'll need to update the App_Start\Ip2CountryStartup.cs file with:
- Your account name
- Your API key
- The container name you placed the file in
- The filename
-
Add the following table to your Sql Server database:
CREATE TABLE IpRecords ( StartAddress VARBINARY(16) NOT NULL CONSTRAINT PK_IpRecords_StartAddress PRIMARY KEY, EndAddress VARBINARY(16) NOT NULL, CountryCode CHAR(2) NOT NULL )
-
Test it out! May I recommend the following test action:
public ActionResult TestIPLookup(string ip) { var result = IpLookup.Current.GetCountryFromIp(ip); return Json(new { CountryCode = result, CountryName = new RegionInfo(result).DisplayName }, JsonRequestBehavior.AllowGet); }
When your application starts, the library will attempt to access the database and check for existing records. If none are found it:
- Downloads the csv file from Azure Storage
- Converts the IP Addresses to IP6 compatible Binary format
- Stores them in the database
You can tweet me at @BenWhoLikesBeer for help or visit BenCull.com