2011-01-07 2 views
2

나는 C# 웹 서비스를 만들었습니다. 나는 모든 사람들이 내 웹 서비스에 전화하기를 원하지 않는다. 나는 IP를 얻는 것이 몇 가지 방법을 수행 할 수 있다고 생각합니다. 사람이 IP 또는 Request.UserHostAddressC# webservice에 요청한 클라이언트의 IP를 가져 오는 방법

는 그런보고 테스트하는 또 다른 방법

+0

IP 주소는 가짜 일 수 있습니다. ** 귀하의 서비스가 100 % 보장되고 싶다면 ** ** IP 보호 만 사용하지 마십시오. – jgauffin

+0

[WebMethod에서 발신자의 IP 주소를 얻으려면 어떻게해야합니까?] (http://stackoverflow.com/questions/130328/how-do-i-get-the-callers-ip-address-in-a) -webmethod) – Liam

답변

1

잡아 요청 객체의 IP 주소로 Webservice를 보호하기 위해 나에게 방법을 말할 수있는 경우는 허용 된 IP 주소와 같은, 그것은 컨텐츠를하지 않을 경우 제공하는 경우 당신이 그것을 얻을 수있는 ASPX 페이지에서

Context.Request.ServerVariables["REMOTE_ADDR"]; 

하십시오 HTTP 403 상태 코드를 반환

5

웹 서비스에서 그것이 (IIS는 추가 정보를 제공 할 경우 거부 IP 주소 403.6있다)

Request.UserHostAddress(); 

업데이트 : 다시 적절한 IP를 얻을 수 있도록 기회를 높이기 위해이 두 클래스를 추가 .. 때문에 프록시 등의 비어있을 수 있습니다. 그냥 경고.이 헤더는 조작하기가 쉽고 보안을 위해 100 %가 아닙니다. (참고로이 코드는 어디에서 왔지만 소스를 기억할 수 있습니다.)

public string DetermineIP(HttpContext context) 
{ 
    if (context.Request.ServerVariables.AllKeys.Contains("HTTP_CLIENT_IP") && CheckIP(context.Request.ServerVariables["HTTP_CLIENT_IP"])) 
    return 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 ip.Trim(); 

    if (context.Request.ServerVariables.AllKeys.Contains("HTTP_X_FORWARDED") && CheckIP(context.Request.ServerVariables["HTTP_X_FORWARDED"])) 
    return 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 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 context.Request.ServerVariables["HTTP_FORWARDED_FOR"]; 

    if (context.Request.ServerVariables.AllKeys.Contains("HTTP_FORWARDED") && CheckIP(context.Request.ServerVariables["HTTP_FORWARDED"])) 
    return context.Request.ServerVariables["HTTP_FORWARDED"]; 

    return context.Request.ServerVariables["REMOTE_ADDR"]; 
} 

    private bool CheckIP(string ip) 
{ 
    if (!String.IsNullOrEmpty(ip)) 
    { 
    long ipToLong = -1; 
    //Is it valid IP address 
    if (TryConvertIPToLong(ip, out ipToLong)) 
    { 
     //Does it fall within a private network range 
     foreach (long[] privateIp in _privateIps) 
     if ((ipToLong >= privateIp[0]) && (ipToLong <= privateIp[1])) 
      return false; 
     return true; 
    } 
    else 
     return false; 
    } 
    else 
    return false; 
} 





private bool TryConvertIPToLong(string ip, out long ipToLong) 
{ 
    try 
    { 
    ipToLong = ConvertIPToLong(ip); 
    return true; 
    } 
    catch 
    { 
    ipToLong = -1; 
    return false; 
    } 
} 

private long ConvertIPToLong(string ip) 
{ 
    string[] ipSplit = ip.Split('.'); 
    return (16777216 * Convert.ToInt32(ipSplit[0]) + 65536 * Convert.ToInt32(ipSplit[1]) + 256 * Convert.ToInt32(ipSplit[2]) + Convert.ToInt32(ipSplit[3])); 
} 


    private long[][] _privateIps = new long[][] { 
    new long[] {ConvertIPToLong("0.0.0.0"), ConvertIPToLong("2.255.255.255")}, 
    new long[] {ConvertIPToLong("10.0.0.0"), ConvertIPToLong("10.255.255.255")}, 
    new long[] {ConvertIPToLong("127.0.0.0"), ConvertIPToLong("127.255.255.255")}, 
    new long[] {ConvertIPToLong("169.254.0.0"), ConvertIPToLong("169.254.255.255")}, 
    new long[] {ConvertIPToLong("172.16.0.0"), ConvertIPToLong("172.31.255.255")}, 
    new long[] {ConvertIPToLong("192.0.2.0"), ConvertIPToLong("192.0.2.255")}, 
    new long[] {ConvertIPToLong("192.168.0.0"), ConvertIPToLong("192.168.255.255")}, 
    new long[] {ConvertIPToLong("255.255.255.0"), ConvertIPToLong("255.255.255.255")} 
}; 
+0

다음과 같이 somethink를 반환합니다. :: 1 클라이언트가 아닙니다. – AEMLoviji

+0

위 업데이트로 보입니다. – StefanE

+0

_privateIps 변수를 찾을 수 없습니다. – AEMLoviji

관련 문제