Click here to Skip to main content
15,891,680 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I tried below codes to get IP from client machine but fail to get the appropriate IP

CODE1

Using this code i am getting IP 100.43.0.212

of my server where i have uploaded my website
C#
private void IPAddress()
        {
            IPHostEntry host;
            string localIP = "?";
            host = Dns.GetHostEntry(Dns.GetHostName());
 
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily.ToString() == "InterNetwork")
                {
                    localIP = ip.ToString();
                    TxtExt.Text = localIP;
                }
            }
        }
JavaScript
Using thic code i am getting 87.101.226.125 which is unknown

<script type="text/javascript">
         window.onload = function () {
             var script = document.createElement("script");
             script.type = "text/javascript";
             script.src = "http://www.telize.com/jsonip?callback=DisplayIP";
             document.getElementsByTagName("head")[0].appendChild(script);
         };
         function DisplayIP(response) {
             document.getElementById("ipaddress").innerHTML = "Your IP Address is " + response.ip;
         }
</script>


using this code i am getting IP of my proxy server 100.43.100.101


C#
public string GetIP4Address()
  {
      string IP4Address = String.Empty;

      foreach (IPAddress IPA in Dns.GetHostAddresses(this.Request.ServerVariables["REMOTE_ADDR"].ToString()))
      {
          if (IPA.AddressFamily.ToString() == "InterNetwork")
          {
              IP4Address = IPA.ToString();
              break;
          }
      }

      if (IP4Address != String.Empty)
      {
          return IP4Address;
      }

      foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
      {
          if (IPA.AddressFamily.ToString() == "InterNetwork")
          {
              IP4Address = IPA.ToString();
              break;
          }
      }

      return IP4Address;

  }


but My local IP address is 100.43.1.16
Posted
Comments
Sergey Alexandrovich Kryukov 27-May-14 2:15am    
Yes, you can get it, but I wonder: why?
—SA

Try:
string address = request.UserHostAddress;
 
Share this answer
 
Comments
$ultaNn 14-Aug-14 2:43am    
i have proxy server its giving me proxy ip not my machine IP
OriginalGriff 14-Aug-14 3:05am    
That's what a proxy is supposed to do...

You can try checking the Forwarded address - but very few proxies set it, because it pretty much makes using a proxy rather pointless!
There is some code here that does that:
http://nice-tutorials.blogspot.co.uk/2011/07/how-to-get-ip-address-in-aspnet-using-c.html

But if that doesn't work, it's by design...
Hi,

Try following.

JavaScript
<script type="text/javascript" src="Scripts/jquery-1.6.2.js"></script>

 <script type="text/javascript">

     $.getJSON("http://jsonip.appspot.com?callback=?",

    function (data) {

        alert("Your ip: " + data.ip);

    });

 </script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900