To get country/geo location of a user using C# MVC, client IP

- Pranay on Jul 03 '15 at 07:09
- viewed 18557 times

If you would like to know the geo location of the client who is visiting your website or to show some specific content based on visitor country/location then this article helps you.

To know the country name, we need to get the IP address of the user and check with the Geo location database to get the location/country based on the IP address. The geo location database is available with many geo location providers. Either we can run that instance in our own server or call the geo location provider API to get the location details.

Note: All the Geolocation providers get the location details based on the IP address. So if the user if spoofing his IP address, then we might get a different location.

Although there are many geo location providers in the market, in this example we will consider the freegeoip.net (http://freegeoip.net/) API to get the geolocation. freegeoip.net is free API and it is limited to 10k queries per hour by default. If you think you might send more requests, then you can download the instance and run on your own server. It’s completely free.

Steps

1. Getting the client IP address.

Use the below code to get the client IP address. i.e., the IP address of the machine which requested a page in your website.

String UserIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(UserIP))
{
    UserIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}

2. Calling the freegeoip.net API

Below is the format we need to use for the API URL.

freegeoip.net/{format}/{IP_or_hostname}

format –> is the response output format. It could be CSV or XML or JSON.

For example, if we would like to get the response in JSON format then we could use the API URL as 'http://freegeoip.net/json/clientIPAddress_here'

string url = "http://freegeoip.net/json/" + UserIP.ToString();
WebClient client = new WebClient();
string jsonstring = client.DownloadString(url);

3. Reading the Geo location details from the response.

To get the sample response format, run this http://freegeoip.net/json/XX.XX.XX.XX (replace XX.XX.XX.XX with the required ip address) URL directly in the browser. You will see the below response format.

{"ip":"xxx.xxx.xxx.xxx","country_code":"","country_name":"","region_code":"","region_name":"","city":"","zip_code":"","time_zone":"","latitude":0,"longitude":0,"metro_code":0}

Use the below code to convert the response to JSON object and then read the values of JSON object and save to a session variable or some other variable.

dynamic dynObj = JsonConvert.DeserializeObject(jsonstring);
System.Web.HttpContext.Current.Session["LocId"] = dynObj.country_code;

Final Code

String UserIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(UserIP))
{
    UserIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
string url = "http://freegeoip.net/json/" + UserIP.ToString();
WebClient client = new WebClient();
string jsonstring = client.DownloadString(url);
dynamic dynObj = JsonConvert.DeserializeObject(jsonstring);
System.Web.HttpContext.Current.Session["UserCountryCode"] = dynObj.country_code;


About the author

Pranay
Sitecore Certified Professional

A Software Engineer by profession, a part time blogger and an enthusiast programmer. You can find more about me here.



Leave your comments on this post here