March 26, 2009

How to get the IP Address and redirect users by country

Download the following dll to get the country by passing IP address. This is a .NET 3.5 library that retrieves the country information for an Internet Protocol version 4 (IPv4) Address, by extending the System.Net.IPAddress class.

http://www.codeplex.com/IPAddressExtensions

 

Then simply add the code in page load method.

if (!IsPostBack) {
            IPAddress ip = IPAddress.Parse(HttpContext.Current.Request.UserHostAddress);
            string country = ip.Country();
            if (country.ToLower() == "india") {
                Response.Redirect("~/indiahomepage.htm");
            }
        }