SIGN UP MEMBER LOGIN:    
ARTICLE

Find Client IP Address And Location in ASP.NET

Posted by Krishna Garad Articles | ASP.NET Programming June 30, 2011
In this article we will see how to find the IP address of the client and the location of the specified IP address.
Reader Level:
Download Files:
 


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.

Login to add your contents and source code to this article
share this article :
post comment
 

Your welcome ashok. please keep reading

Posted by Krishna Garad Jan 13, 2012

Thanks krishna your article is so helpful to other i always wait for your article.

Posted by ashok kumar Jan 12, 2012

Hi ashok check this one for the same here http://www.c-sharpcorner.com/uploadfile/krishnasarala/track-client-location-from-ip-address-in-Asp-Net/

Posted by Krishna Garad Jan 11, 2012

I have used break point and tried many times but every time getting error at the line WebResponse rep = rssReq.GetResponse(); the error is "The remote server returned an error: (503) Server Unavailable." by which it throw exception. how can i solve this problem. Is there any need to registration for using this web service.

Posted by ashok kumar Jan 10, 2012

I think you have to put the break point and then check it out.

Posted by Krishna Garad Jan 09, 2012
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor