How to get the IP address from a visitor (also if behind a Proxy) [duplicate]
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.
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
}
so simple dude,
string IP = Request.ServerVariables["REMOTE_HOST"].ToString();
精彩评论