我已經創建了C#web服務。我不希望每個人都打電話給我的web服務。我認爲讓ip可以保護執行某些方法。誰能告訴我的方式來保護web服務與IP或另一種方式如何檢索向C#webservice發出請求的客戶端的IP
2
A
回答
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")}
};
相關問題
- 1. 如何向客戶端發出請求IP
- 2. 向Google的C++本地客戶端發送POST請求
- 3. 如何服務器請求從客戶端檢索Python程序,從客戶端
- 4. 如何檢索向網頁發出請求的客戶端的ipv4和ipv6地址
- 5. 記錄有關客戶端向WCF(WAS)發送請求的信息webservice
- 6. 如何識別Flash與JavaScript客戶端發出的請求?
- 7. 我如何確保只使用我的客戶端客戶端向我的服務器發出請求?
- 8. fiddler - 我可以輸出請求的客戶端ip /名稱嗎?
- 9. 如何獲取請求Tor WebPage的客戶端IP?
- 10. 如何獲取Tornado請求的客戶端IP?
- 11. 如何獲取Tornado Web套接字請求的客戶端IP?
- 12. Javascript網站總是向客戶的IP發送請求?
- 13. 如何使用客戶端Javascript向Ldap發送請求?
- 14. 如何阻止客戶端向服務器發送ajax請求?
- 15. 使用java向WebService發出SOAP請求
- 16. 如何開發JAX-WS webservice客戶端?
- 17. PHP請求檢查客戶端是否可通過IP
- 18. Cakephp向webservice發送請求
- 19. 當客戶端向通過ISP的網站發出請求時發送的IP地址是什麼
- 20. 如何從C#客戶端發送修補程序請求?
- 21. 需要檢索從Facebook的用戶ID c#sdk沒有向Facebook發出請求
- 22. 使用Java小程序發送請求與客戶端IP
- 23. 如何在python中知道IP Lan請求客戶端?
- 24. 如何使用Restify從請求對象獲取客戶端IP?
- 25. cURL通過客戶端或用戶IP的請求
- 26. Jersey客戶端:向請求添加Cookie
- 27. 作爲HTTP請求的回調向客戶端發送錯誤
- 28. 從本地主機上的webservice客戶端調用WCF webservice發出的請求超時錯誤
- 29. 檢測客戶端中止請求Jersey
- 30. WCF如何重定向客戶端的請求
IP地址可以僞造。所以,不要只使用** ** IP保護,如果你想100%確定你的服務受到保護。 – jgauffin 2011-01-07 11:13:53
可能的重複[如何獲取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