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:
- CSV - Comma Separated Values
- XML - Extensible Markup Language
- 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.

Lokesh JatPosted Jan 28, 2019, 12:27 AM
It is not Working Throw Error
G Krishna RaoPosted Dec 6, 2017, 3:09 AM
My machine is LAN connected and able to get IP address but based on that IP i want to display country name.please help me..
Kayastha Iswor LalPosted Jun 29, 2017, 2:15 AM
I just want to show Ip address only. how can those unnecessary stuff can be removed????
Riya MahantyPosted May 17, 2017, 9:35 AM
You code not working getting error The remote server returned an error: (404) Not Found
Gaurav AnandPosted Jan 10, 2017, 6:19 AM
Copy and paste the code(given in above comment) in cs page (Default.aspx.cs) downloaded code.
Gaurav AnandPosted Jan 10, 2017, 6:17 AM
Using System;using System.Collections.Generic;using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using System.Net; using System.Xml; using System.Data; using System.Text; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { try { if (!IsPostBack) { string myExternalIP; myExternalIP = GetExternalIp(); lblip.Text = myExternalIP.ToString(); DataTable dt = GetLocation(myExternalIP.ToString()); lblcountry.Text = dt.Rows[0]["CountryName"].ToString(); lblregion.Text = dt.Rows[0]["RegionName"].ToString(); lblcity.Text = dt.Rows[0]["City"].ToString(); } } catch (Exception) { throw; } } private string GetExternalIp() { string outputIP = string.Empty; HttpWebRequest request = WebRequest.Create("https://api.ipify.org/?format=text&callback=getip") as HttpWebRequest; //request.Accept = "application/xrds+xml"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); WebHeaderCollection header = response.Headers; var encoding = ASCIIEncoding.ASCII; using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding)) { string responseText = reader.ReadToEnd(); outputIP = responseText; } return outputIP; } private DataTable GetLocation(string ipaddress) { //Create a WebRequest WebRequest rssReq = WebRequest.Create("http://freegeoip.net/xml/" + ipaddress); //Create a Proxy WebProxy px = new WebProxy("http://freegeoip.net/xml/" + ipaddress, true); //Assign the proxy to the WebRequest rssReq.Proxy = px; //Set the timeout in Seconds for the WebRequest rssReq.Timeout = 2000; try { //Get the WebResponse WebResponse rep = rssReq.GetResponse(); //Read the Response in a XMLTextReader XmlTextReader xtr = new XmlTextReader(rep.GetResponseStream()); //Create a new DataSet DataSet ds = new DataSet(); //Read the Response into the DataSet ds.ReadXml(xtr); return ds.Tables[0]; } catch { return null; } } }
Arpit SrivastavaPosted Jun 16, 2016, 1:08 AM
string IP = ""; string strHostName = ""; strHostName = System.Net.Dns.GetHostName(); IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName); IPAddress[] addr = ipEntry.AddressList; IP = addr[3].ToString(); XmlDocument doc = new XmlDocument(); doc.Load("http://www.freegeoip.net/xml"); XmlNodeList nodeLstCity = doc.GetElementsByTagName("City"); IP = "" + nodeLstCity[0].InnerText + "<br>" + IP; Response.Write(IP); lblIP.Text =IP; Session["IP"] =IP; Label1.Text = strHostName; Session["strhostname"] = strHostName;
sangram bhorePosted Jun 24, 2015, 1:33 AM
its nice
ticky tikPosted Apr 20, 2015, 9:46 PM
it's does't works for me
Tejpal SinghPosted Jan 7, 2015, 1:41 AM
You can use HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] to find Ip address of client
Sumit KumarPosted Aug 22, 2014, 11:10 AM
It is not working as explained. It is failed to find the client IP address so in the next line it is thrown an exception of object reference is not set an instance of the object. It is failed every time and not worked. So can you provide the alternate solution to find the client IP address, location? Thanks.
suriyaPosted Nov 5, 2013, 3:12 AM
otha
Amogh NatuPosted Aug 2, 2013, 3:19 AM
Not working. It is just displaying a label "Your IP address : " and an image which we are not able to see.
Elena josephPosted Jul 26, 2013, 5:37 AM
its not working
harshal yelpalePosted Apr 15, 2013, 2:15 AM
Not working. It says The remote server returned an error: (503) Server Unavailable on the line "WebResponse rep = rssReq.GetResponse();"
irfan ahmadPosted Mar 23, 2013, 7:37 AM
Edit This programm is not working.It says object reference is not set an instance of the object
irfan ahmadPosted Mar 23, 2013, 7:25 AM
This programm is not working.It says object not found.
sumit jaiswalPosted Jan 8, 2013, 5:31 AM
When I upload this page on server geting the error Object reference not set to an instance of an object. please help
sumit jaiswalPosted Jan 8, 2013, 5:31 AM
When I upload this page on server geting the error Object reference not set to an instance of an object. please help
sumit jaiswalPosted Jan 8, 2013, 5:31 AM
When I upload this page on server geting the error Object reference not set to an instance of an object. please help
vstarPosted Nov 2, 2012, 1:59 PM
This post helped me to resolve the same issue. I hope this can help you out. http://dotnetstock.com/technical/how-to-get-ip-address-of-a-client-system-using-asp-net
ashok kumarPosted Jan 10, 2012, 12:45 AM
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.
ashok kumarPosted Jan 9, 2012, 5:37 AM
Hi Krishna, During running ur uploaded file i m getting data table but it dosent have any name in country and etc. and some time i get error like Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 55: { Line 56: Line 57: throw; Line 58: } Line 59: } if u have some time then please help me that how can i use this.
Ethan WooPosted Jul 1, 2011, 5:41 AM
Client Web Browser -----> Web Server (Your HttpWebRequest is created here, so it is a request send out of your Web Server, 100% not Client Web Browser) ------> whatismyip Server. Right?
Dinesh BeniwalPosted Jul 1, 2011, 1:26 AM
Krishna very useful article, I always wait for your articles, thanks
Rohatash KumarPosted Jul 1, 2011, 12:17 AM
Good article, thanks
Shalini JunejaPosted Jun 30, 2011, 8:12 AM
Helpful Article