Find Client IP Address And Location in ASP.NET


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 (  and ( . 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 which will return the IP address of the client on the network and next to get the details we made a request to 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 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 =


                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 =


                using (System.IO.StreamReader reader = new


                    myExternalIP = reader.ReadToEnd();


                lblip.Text = myExternalIP.ToString();

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

For this we have to use the webservice from 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("" + ipaddress);
    WebProxy px = new WebProxy("" + ipaddress, true);
    rssReq.Proxy = px;      
    rssReq.Timeout = 2000;
        WebResponse rep = rssReq.GetResponse();
        XmlTextReader xtr = new XmlTextReader(rep.GetResponseStream());
        DataSet ds = new DataSet();
        return ds.Tables[0];
        return null;


In this way we can track our client very easily.

Up Next
    Ebook Download
    View all
    View all