记录这两个或其中一个的最佳做法? (1)" HTTP_X_FORWARDED_FOR" (2)" REMOTE_ADDR"

时间:2016-01-12 17:04:13

标签: c# asp.net logging ip-address httpcontext

我修改了以下代码:How to Get IP Address?

static string IPForwarded =
  string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"])
  ? null : HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(',').Last().Trim(); 

static string IPRemoteAddress = 
  string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"])
  ? null : HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

// Returns a simple message with both IPs
private static string GetIPMessage() { return 
    (String.IsNullOrWhiteSpace(IPForwarded) ? "" : "[HTTP_XFF " + IPForwarded + "] ") + 
    (String.IsNullOrWhiteSpace(IPRemoteAddress) ? "" : "[REMOTE_ADDR " + IPRemoteAddress + "] ");
}

在原始代码的大多数变体中,如果它不为空,则返回列表HTTP_X_FORWARDED_FOR中的最后一个IP。或者,它返回REMOTE_ADDR。有没有我应该记录两者的情况(正如我在GETIPMessage()中所做的那样可能的IP阻塞?换句话说,它们是否相互排斥?如果另一个变量存在,一个变量是空的还是无用的?

0 个答案:

没有答案