Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the IP address from a visitor (also if behind a Proxy) [duplicate]

Tags:

c#

asp.net

proxy

ip

Possible Duplicate:
Client IP using C#

I use ASP.net and C# and I would like to know how to get the IP address from a visitor on a page.

I would like to see an example of code that retrieves the IP address and also will be able to show if an IP was behind a proxy.

Thanks for your time.

like image 956
GibboK Avatar asked Sep 07 '11 08:09

GibboK


1 Answers

You could use the UserHostName property on the Request object:

string ip = Request.UserHostName;

As far as your second question about the proxy is concerned, there is no reliable way to achieve this. You could use heuristics to look for some HTTP request headers that might be sent by the proxy server such as Via or X-Forwarded-For.

string header = Request.Headers["Via"] ?? Request.Headers["X-Forwarded-For"];
if (!string.IsNullOrEmpty(header))
{
    // probably the request was forwarded from a proxy server
    // but you cannot be 100% sure as HTTP request headers can be faked
}
like image 105
Darin Dimitrov Avatar answered Oct 22 '22 02:10

Darin Dimitrov