0

I have a web application that has a login. Now what I want is to get the IP address of that end user upon login. I've tried researching but based on what I understood, it gets the ip of my machine not the person that logins. This app is already deployed on a server. And if I do what they said, I will be getting the server's ip address. But I want the IP address of the end user that logs in.

This is a sample of what I've seen.

private string GetIP()
{
    string strHostName = "";
    strHostName = System.Net.Dns.GetHostName();

    IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);

    string ipaddress =convert.tostring(ipEntry.AddressList[2]);

    return ipaddress.tostring();

}

I'm not sure if I understood this correctly but I think this will get local ip address, not the ip address of the end user that is logging in. Please do correct me if I'm wrong. Any ideas? Thanks!

J0e3gan
  • 8,570
  • 9
  • 52
  • 78
Luke Villanueva
  • 1,992
  • 7
  • 41
  • 92
  • 3
    possible duplicate of [How to get Public Ip address of a user in C#](http://stackoverflow.com/questions/19285957/how-to-get-public-ip-address-of-a-user-in-c-sharp) –  Aug 05 '14 at 06:24

3 Answers3

9

Use -

HttpContext.Current.Request.UserHostAddress
Subha
  • 1,041
  • 6
  • 12
3

Try both :

HttpContext.Current.Request.UserHostAddress;

or

HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
SanDeep
  • 44
  • 1
1

you can use REMOTE_ADDR and HTTP_X_FORWARDED_FOR from Request.ServerVariables .

The UserHostAddress property is simply a wrapper around the REMOTE_ADDR server variable. So if you user is behind a proxy (or router), this returns only one IP Address, the IP Address of the proxy (or router) .

Red : http://haacked.com/archive/2006/10/11/A_Gotcha_Identifying_the_Users_IP_Address.aspx/

There is another thread in stackoverflow : How to Get IP Address?

Community
  • 1
  • 1
Rabi
  • 2,180
  • 16
  • 18