Find Client IP Address And Location in ASP.NET



Introduction:

In this article we will see how to find the IP address of the client and the location of the specified IP address. This kind of thing can be done to provide security to an ASP.NET application. In this article we are using some sites which provide the IP and address details of IP address (http://www.whatismyip.org/)  and (http://freegeoip.appspot.com/) . If you hit the above url you can see your IP address and the exact location where are you.

Before finding location details we need the IP address of the client so first we find the client IP address by making a webrequest to http://www.whatismyip.org/ which will return the IP address of the client on the network and next to get the details we made a request to http://freegeoip.appspot.com which is the webservice which will provide the location details of the specified IP address. This webservice will return the details in various formats:

  1. CSV
    Comma Seprated Values
  2. XML
    Extensible Markup Language
  3. JSON
    JavaScript Object Notation

To get the details of the client location we are sending a request with the client IP address and getting the response in XML format.

How To Get the IP Address Of the Client?

For getting the IP Address of the client we have to make a request to http://www.whatismyip.org. Write the following code in your page_load event to get the IP Address of the Client.

            
string myExternalIP;
                string strHostName = System.Net.Dns.GetHostName();
                string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
                string clientip = clientIPAddress.ToString();
                System.Net.HttpWebRequest request =

            (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("http://www.whatismyip.org");

                request.UserAgent = "User-Agent: Mozilla/4.0 (compatible; MSIE" +

                    "6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";

                System.Net.HttpWebResponse response =

                (System.Net.HttpWebResponse)request.GetResponse();

                using (System.IO.StreamReader reader = new

                StreamReader(response.GetResponseStream()))
                {

                    myExternalIP = reader.ReadToEnd();

                    reader.Close();

                }
                lblip.Text = myExternalIP.ToString();

How To Get the Location On The Basis Of IP Address?

For this we have to use the webservice from http://freegeoip.appspot.com/. For doing this we have written the GetLocation mathod which will take an IP address as the argument and will return the datatable object.

private DataTable GetLocation(string ipaddress)
{
    WebRequest rssReq = WebRequest.Create("http://freegeoip.appspot.com/xml/" + ipaddress);
    WebProxy px = new WebProxy("http://freegeoip.appspot.com/xml/" + ipaddress, true);
    rssReq.Proxy = px;      
    rssReq.Timeout = 2000;
    try
    {
        WebResponse rep = rssReq.GetResponse();
        XmlTextReader xtr = new XmlTextReader(rep.GetResponseStream());
        DataSet ds = new DataSet();
        ds.ReadXml(xtr);
        return ds.Tables[0];
    }
    catch
    {
        return null;
    }
}

Conclusion:

In this way we can track our client very easily.

Up Next
    Ebook Download
    View all
    Learn
    View all