How to Get IP Address and Location of Visitor using C#

Here, in this blog I will explain you how to get IP address and location of visitors who are accessing your web page on the internet.

Actually Internet protocol is used to track the system and provide use system host and system IP and location. To use this code you can just figure out from where and from which IP address my site is being accessed.

For this you need to create a static class where you will get Visitor Details and location as well.

  1. public class IPHostGenerator    
  2. {    
  3.     internal string GetCurrentPageUrl()    
  4.     {    
  5.         return HttpContext.Current.Request.Url.AbsoluteUri;    
  6.     }    
  7.     internal string GetVisitorDetails()    
  8.     {    
  9.         string varIPAddress = string.Empty;    
  10.         string varVisitorCountry = string.Empty;    
  11.         string varIpAddress = string.Empty;    
  12.         varIpAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];    
  13.         if (string.IsNullOrEmpty(varIpAddress))    
  14.         {    
  15.             if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)    
  16.             {    
  17.                 varIpAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];    
  18.             }    
  19.         }    
  20.   
  21.         //varIPAddress = (System.Web.UI.Page)Request.ServerVariables["HTTP_X_FORWARDED_FOR"];    
  22.         if (varIPAddress == "" || varIPAddress == null)    
  23.         {    
  24.             if (HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] != null)    
  25.             {    
  26.                 varIpAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];    
  27.             }    
  28.         }    
  29.         //varIPAddress = Request.ServerVariables["REMOTE_ADDR"];    
  30.         return varIpAddress;    
  31.     }    
  32.   
  33.     internal DataTable GetLocation(string varIPAddress)    
  34.     {    
  35.         WebRequest varWebRequest = WebRequest.Create("http://freegeoip.net/xml/" + varIPAddress);    
  36.         WebProxy px = new WebProxy("http://freegeoip.net/xml/" + varIPAddress, true);    
  37.         varWebRequest.Proxy = px;    
  38.         varWebRequest.Timeout = 2000;    
  39.         try    
  40.         {    
  41.             WebResponse rep = varWebRequest.GetResponse();    
  42.             XmlTextReader xtr = new XmlTextReader(rep.GetResponseStream());    
  43.             DataSet ds = new DataSet();    
  44.             ds.ReadXml(xtr);    
  45.             return ds.Tables[0];    
  46.         }    
  47.         catch    
  48.         {    
  49.             return null;    
  50.         }    
  51.     }    
  52.   
  53.     internal string GetMachineNameUsingIPAddress(string varIpAdress)    
  54.     {    
  55.         string machineName = string.Empty;    
  56.         try    
  57.         {    
  58.             IPHostEntry hostEntry = Dns.GetHostEntry(varIpAdress);    
  59.   
  60.             machineName = hostEntry.HostName;    
  61.         }    
  62.         catch (Exception ex)    
  63.         {    
  64.             // Machine not found...    
  65.         }    
  66.         return machineName;    
  67.     }    
  68. }   

 

Hope you enjoyed this blog. thanks
Next Recommended Reading Get Client IP Address In ASP.NET MVC