2011-01-07 30 views
2

我已經創建了C#web服務。我不希望每個人都打電話給我的web服務。我認爲讓ip可以保護執行某些方法。誰能告訴我的方式來保護web服務與IP或另一種方式如何檢索向C#webservice發出請求的客戶端的IP

+0

IP地址可以僞造。所以,不要只使用** ** IP保護,如果你想100%確定你的服務受到保護。 – jgauffin 2011-01-07 11:13:53

+0

可能的重複[如何獲取WebMethod中的調用者的IP地址?](http://stackoverflow.com/questions/130328/how-do-i-get-the-callers-ip-address-in-a -webmethod) – Liam 2014-01-09 11:39:48

回答

1

抓鬥從請求對象的IP地址Request.UserHostAddress

然後進行測試,看看它等於你允許的IP地址,如果是服務的內容,如果沒有返回一個HTTP 403狀態碼(IIS有403.6對,如果你想給更多的信息拒絕IP地址)

5

在一個Web服務,它是:

Context.Request.ServerVariables["REMOTE_ADDR"]; 

從一個ASPX頁面,您可以用得到它:

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")} 
}; 
相關問題