title: How to Find User's IP address
body: <p>I need help to find user's IP address who are browsing to my site (using C# Code).</p>
  
  <p>I am confused about system IP address, ISP IP address, Network address and what is IPV4 and IPV6.</p>
  
  <p>Below is the code I got from other source:</p>
  
  <pre><code>protected string GetUserIP()
  {
          string userIP = string.Empty;
          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)
              {
                  userIP = addresses[0];
              }
          }
          else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
          {
              userIP = HttpContext.Current.Request.UserHostAddress;
          }
  
          return userIP;
  }
  </code></pre>
  
  <p>But here I am not getting the expected result.</p>
  
  <p>When test this in my local-host I got 127.0.0.1</p>
  
  <p>When I deploy this into my development server and browse the page in my local machine, I got different address.</p>
  
  <p>I tried accessing the same URL in other machine and IP address are same.</p>
  
  <p>The systems I am using to access my development server deployed page in in same network.</p>
  
  <p>So is it the network IP my code returned..?</p>
  
  <p>Because when I find my IP using ipconfig command it's again a different!</p>
  
  <p>And when I checked my IP using 3rd party site (<a href="http://www.whatismyip.com/ip-address-lookup/" rel="nofollow">http://www.whatismyip.com/ip-address-lookup/</a>), it shows the actual IP I expected. How can I get that actual IP using C# code?</p>
  
  <p>My ultimate goal is, get the user location details who is browsing my site using Maxmind City DB, but for that I need to pass the IP of browsing user.</p>
  
  <pre><code>string ip = GetUserIP();
  string db = “GeoIP2-City.mmdb";
  var reader = new DatabaseReader(db);
  var city = reader.City(ip);
  double? lat = city.Location.Latitude;
  double? lang = city.Location.Longitude;
  </code></pre>
  
  <p>For the IP I got from my above code, I got exception (IP not found in Database) when I tried to get information from Maxmind DB, 
  but for the IP I got from "whatismyip" website, I got the details from the Maxmind DB.</p>
  
  <p>Hope My question is clear.
  Please let me know if anyone have any inputs.</p>
  
  <p>Thanks,
  Sharath</p>