摘要:[ASP.NET] 幾種取得IP的方法
簡單記錄一下,或許有時後會出現一些奇怪的東西也說不定
像是它會跑出::1 這種結果,還需要深入研究才知道
使用HTTP_X_FORWARDED_FOR
HttpContext context = HttpContext.Current;
if (context == null)
{
return string.Empty;
}
HttpRequest request = context.Request;
if (request == null)
{
return string.Empty;
}
return request.ServerVariables["HTTP_X_FORWARDED_FOR"];
使用REMOTE_ADDR
HttpContext context = HttpContext.Current;
if (context == null)
{
return string.Empty;
}
HttpRequest request = context.Request;
if (request == null)
{
return string.Empty;
}
return request.ServerVariables["REMOTE_ADDR"];
使用UserHostAddress
HttpContext context = HttpContext.Current;
if (context == null)
{
return string.Empty;
}
HttpRequest request = context.Request;
if (request == null)
{
return string.Empty;
}
return request.UserHostAddress;