1

I try to get IP-address and port of client which sends HTTP requests to the server. I used properties of HttpListenerRequest class. UserHostAddress property returns ip and port of server but not client. RemoteEndPoint returns some ip and port but it is not true client ip (I know that client requests are coming from port 1234) and every time it returns different data (I don't know why). How can I solve this? May be I should set http header From on the client side?

Thank you very much!

user3403730
  • 101
  • 1
  • 2
  • 11
  • 1
    saw this ? http://stackoverflow.com/questions/735350/how-to-get-a-users-client-ip-address-in-asp-net – Zaffar Saffee Jan 03 '15 at 01:26
  • How do you know it's the wrong IP? You will likely get their outside facing IP, and depending on the network set-up it too may very. IP only really designates 'network'. What is behind that ip/network can be other networks or computers or evening cell phones – terary Jan 03 '15 at 01:29
  • terary, I tested my application at the same machine (client and server are running at the same machine). And client port number and port received on server are not the same. – user3403730 Jan 03 '15 at 01:34

1 Answers1

4

based on SO answer, I think this would help you...

   protected void GetUser_IP()
{
    string VisitorsIPAddr = string.Empty;
    if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
    {
        VisitorsIPAddr = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
    }
    else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
    {
        VisitorsIPAddr = HttpContext.Current.Request.UserHostAddress;
    }
    uip.Text = "Your IP is: " + VisitorsIPAddr;
}
Community
  • 1
  • 1
Zaffar Saffee
  • 6,167
  • 5
  • 39
  • 77