Find Client Ip Address And Location In ASP.NET
In this article we will see how to find the IP address of client and the location of specified IP address. This kind of things can be done to provide sequrity 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 addresses and the exact location where are you.
Before finding location details we necessary the IP address of client so first we had finded client IP address by making webrequest to http://www.whatismyip.org which will return the ip address of client on network and next to get the details we made request to http://freegeoip.appspot.com which is the webservice which will provide the location details of specified IP address. This webservice will return the details in various format’s
Comma Seprated Values
Extensible Markup Language
To get the details of client location we are sending request with client IP address and getting the response in XML format.
How To Get IP Address Of Client?
For getting Ip Address of client we have to make a request to http://www.whatismyip.org Write the following code in your page_load event to get Ip Address of Client.
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 Location On The Basis Of Ip Address?
For this task we have to use the webservice from http://freegeoip.appspot.com For performing this task we have written GetLocation mathod which will take Ip address as 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;
WebResponse rep = rssReq.GetResponse();
XmlTextReader xtr = new XmlTextReader(rep.GetResponseStream());
DataSet ds = new DataSet();
In this way we can track our client very easily.