title: How do I get the actual ip address of the visitor instead of his public ip address?
body: <p>I am using this code to get the actual ip address of the visitor my website but it returns the PUBLIC ip address which is same for everybody in our office but instead I want the actual ipv4 address of the visitor's system. </p>
  
  <pre><code>    protected string GetIPAddress()
      {
          System.Web.HttpContext context = System.Web.HttpContext.Current;
          string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  
  
  
  
          if (!string.IsNullOrEmpty(ipAddress))
          {
              string[] addresses = ipAddress.Split(',');
              if (addresses.Length != 0)
              {
                  return addresses[0];
              }
          }
  
          return context.Request.ServerVariables["REMOTE_ADDR"];
      }
  
      protected string GetIPAddressLocal()
      {
          System.Web.HttpContext context = System.Web.HttpContext.Current;
          string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  
          if (!string.IsNullOrEmpty(ipAddress))
          {
              string[] addresses = ipAddress.Split(',');
              if (addresses.Length != 0)
              {
                  return addresses[addresses.Length-1];
              }
          }
  
          return context.Request.ServerVariables["REMOTE_ADDR"];
      }
  
      public static IPAddress GetIPAddress3(string hostName)
      {
          Ping ping = new Ping();
          var replay = ping.Send(hostName);
  
          if (replay.Status == IPStatus.Success)
          {
              return replay.Address;
          }
          return null;
      }
  
      private string GetIP()
      {
          string strHostName = "";
          strHostName = System.Net.Dns.GetHostName();
  
          IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
  
          string ipaddress = "";
  
          return ipaddress.ToString();
  
      }
  
      public static string GetUserIP()
      {
          var ip = (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null
          &amp;&amp; System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != "")
          ? System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]
          : System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
          if (ip.Contains(","))
              ip = ip.Split(',').First().Trim();
          return ip;
      }
  
  
  
  
  
  
  string ipaddress = GetIPAddress();
              ViewBag.ip = ipaddress;
              ViewBag.ip2= System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList.GetValue(1).ToString();
              ViewBag.ip3 = Request.ServerVariables["REMOTE_ADDR"];
              ViewBag.ip4 = System.Web.HttpContext.Current.Request.UserHostAddress;
              ViewBag.ip5 = GetIPAddress3(System.Net.Dns.GetHostName());
              ViewBag.ip6 = GetIP();
              ViewBag.ip7 = GetUserIP();
  </code></pre>
  
  <p>I have used multiple function it returns either the server<code>s ip address or the public ip address which is same for all the users. It doesn</code>t return the actual ip address that the user system has.</p>