Gib*_*boK 5 c# asp.net ip proxy
可能重复:
使用C#的客户端IP
我使用ASP.net和C#,我想知道如何从页面上的访问者获取IP地址.
我想看一个检索IP地址的代码示例,并且还能够显示IP是否在代理后面.
谢谢你的时间.
您可以在Request对象上使用UserHostName属性:
string ip = Request.UserHostName;
Run Code Online (Sandbox Code Playgroud)
至于你关于代理的第二个问题,没有可靠的方法来实现这一点.您可以使用启发式方法查找可能由代理服务器发送的某些HTTP请求标头,例如Via或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
}
Run Code Online (Sandbox Code Playgroud)