문제

사용자가 내 사이트를 방문하는 IP 주소에 대한 통계를 수집하고 172.16.16.1과 172.16.16.248의 두 개의 IP 주소 만 표시되는 것을 발견했습니다. IP 주소를 결정하는 데 사용하는 속성은입니다

Request.UserHostAddress

IP 주소 정보가 잃어버린 이유는 무엇입니까? 모든 사용자는 전 세계에서 왔으므로 두 개의 프록시 뒤에있을 수 없습니다.

도움이 되었습니까?

해결책

이것은 리버스 프록시의 작품처럼 보입니다. 리버스 프록시를 사용하면 클라이언트가 프록시에 연결하여 서버에 대한 새로운 연결이 열립니다. ASP.NET은 들어오는 연결의 정보를 사용하여 사용자 주소를 채우기 때문에 리버스 프록시의 주소를 얻습니다.

실제로이 구성에있는 경우 리버스 프록시의 도움이 필요합니다. 올바른 정보를 얻을 수 있습니다. 대부분의 리버스 프록시는 클라이언트의 실제 IP 주소와 함께 HTTP 요청에 헤더를 추가 할 수 있습니다. 프록시의 문서를 확인하십시오.

다른 팁

당신은 이와 같은 것을 원할 수도 있습니다.

string SourceIP = String.IsNullOrEmpty(Request.ServerVariables["HTTP_X_FORWARDED_FOR"]) ? Request.ServerVariables["REMOTE_ADDR"] : Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(",")[0];

헤더 용 http_x_forwarded_는 프록시 서버 뒤에 IP 주소를 가져옵니다.

왜 더 자세히 설명하는지 설명하는이 페이지를 참조하십시오. 사용자의 실제 IP를 얻습니다

Building on Dave Anderson's answer, here is a snippet that takes into account a chain of reverse proxies.

string forwardedFor = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

string ipStr = string.IsNullOrWhiteSpace(forwardedFor) 
                   ? Request.ServerVariables["REMOTE_ADDR"] 
                   : forwardedFor.Split(',').Select(s => s.Trim()).First();

I assume you are behind a NAT/Reverse Proxy so I think you have to use:

Request.ServerVariables("REMOTE_ADDR") 

Most likely 172.16.0.0/12 is your privat LAN where 172.16.16.248 is your own address and 172.16.16.1 the address of your router/proxy.

Request.ServerVariables("REMOTE_ADDR") isn't work. this problem is because ur server is probably behind some proxy.(or connected to internet via some network) or your router settings are set as NAT (Network Address Translation) this technique doesnt pass ip to server. in such situations u can't get IP address using Asp.net however Java Provide applet using which u can get IP Address in any case.

( for Netscape, Mozilla and Firefox only, and Java must be enabled)

<script language="javascript" type="text/javascript">   

if (navigator.appName.indexOf("Netscape") != -1){
ip = "" + java.net.InetAddress.getLocalHost().getHostAddress();
document.write("<b>Your IP address is " + ip+'</b>');
}
else {
document.write("<b>IP Address only shown in Netscape with Java enabled!</b>");
}

</script>

The two addresses you've listed there are from one of the ranges defined as being private. (see here for description)

It sounds more like you're picking up the internal address of your own firewall(s)?

Building on tomfannings answer...

 public static string ClientIp(this HttpRequestBase @this) {
  var clientIp = string.Empty;
  string forwardedFor = @this.ServerVariables["HTTP_X_FORWARDED_FOR"];

  if (string.IsNullOrWhiteSpace(forwardedFor)) {
    clientIp = @this.ServerVariables["REMOTE_ADDR"];
  } else {

    var array = forwardedFor
      .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
      .Select(s => s.Trim());

    foreach (var element in array) {
      if (element.IsValidIp4() || element.IsValidIp6()) {
        clientIp = element;
        break;
      }
    }
  }
  return clientIp;
}

public static bool IsValidIp4(this string @this) {
  var pattern = new Regex(@"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))$");
  return pattern.IsMatch(@this);
}

public static bool IsValidIp6(this string @this) {
  var pattern = new Regex(@"^s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*(\/(d|dd|1[0-1]d|12[0-8]))$");
  return pattern.IsMatch(@this);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top