Luke Villanueva
Luke Villanueva

Reputation: 2070

Get user IP Address

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!

Upvotes: 0

Views: 8373

Answers (3)

SanDeep
SanDeep

Reputation: 44

Try both :

HttpContext.Current.Request.UserHostAddress;

or

HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

Upvotes: 3

Rabi
Rabi

Reputation: 2220

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?

Upvotes: 1

Subha
Subha

Reputation: 1051

Use -

HttpContext.Current.Request.UserHostAddress

Upvotes: 9

Related Questions