현재 클라이언트의 IP 주소를 가져 오는 RESTful C# 웹 서비스가 있습니다. 이것은로드 밸런싱이 구현 될 때까지 제대로 작동했습니다. 이제 웹 서비스는로드 밸런서 뒤에 있습니다.C#에서 IP 주소 얻기
이제 사용자의 IP 주소를 가져올 때마다로드 균형 조정기의 IP 주소가 대신 표시됩니다. 여기 코드가 어떻게 생겼는지 ...
System.Web.HttpContext context = System.Web.HttpContext.Current;
if (context.Request.ServerVariables.AllKeys.Contains("HTTP_CLIENT_IP") && CheckIP(context.Request.ServerVariables["HTTP_CLIENT_IP"]))
return new Model(context.Request.ServerVariables["HTTP_CLIENT_IP"]);
if (context.Request.ServerVariables.AllKeys.Contains("HTTP_X_FORWARDED_FOR"))
foreach (string ip in context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(','))
if (CheckIP(ip.Trim()))
return new Model(ip.Trim());
if (context.Request.ServerVariables.AllKeys.Contains("HTTP_X_FORWARDED") && CheckIP(context.Request.ServerVariables["HTTP_X_FORWARDED"]))
return new Model(context.Request.ServerVariables["HTTP_X_FORWARDED"]);
if (context.Request.ServerVariables.AllKeys.Contains("HTTP_X_CLUSTER_CLIENT_IP") && CheckIP(context.Request.ServerVariables["HTTP_X_CLUSTER_CLIENT_IP"]))
return new Model(context.Request.ServerVariables["HTTP_X_CLUSTER_CLIENT_IP"]);
if (context.Request.ServerVariables.AllKeys.Contains("HTTP_FORWARDED_FOR") && CheckIP(context.Request.ServerVariables["HTTP_FORWARDED_FOR"]))
return new Model(context.Request.ServerVariables["HTTP_FORWARDED_FOR"]);
if (context.Request.ServerVariables.AllKeys.Contains("HTTP_FORWARDED") && CheckIP(context.Request.ServerVariables["HTTP_FORWARDED"]))
return new Model(context.Request.ServerVariables["HTTP_FORWARDED"]);
return new Model(context.Request.ServerVariables["REMOTE_ADDR"]);
그럼에도 불구하고 나는 여전히 궁극적으로 잘못된 IP 주소를 얻고 있습니다. 이견있는 사람? 아이디어? 내가 놓친 게 있니?
당신은 오는 원시 요청의 추적을받을 수 있습니까? 그렇다면 헤더 중 원래 IP 주소가 포함되어 있습니까? 그렇지 않은 경우이 데이터를 제공하기 위해로드 밸런서를 변경해야합니다. 그렇지 않으면 운이 없어진 것입니다. 그렇다면 헤더를 요청하고 데이터를 가져옵니다. – Tejs
HTTP 헤더에 대해'X-Forwarded-For'를 시도하십시오. – vcsjones