我已经创建了C# web服务。我不想让每个人都调用我的网络服务。我认为获得ip我可以保护执行一些方法。谁能告诉我用ip或其他方式保护Webservice的方法
发布于 2011-01-07 17:15:28
在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 (16777216L * 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")}
};
发布于 2011-01-07 17:15:13
从request对象Request.UserHostAddress
中抓取IP地址
然后测试它是否等于您允许的ip地址,如果不是,则返回HTTP403状态码(如果您想提供更多信息,IIS的403.6表示IP地址已被拒绝)
https://stackoverflow.com/questions/4623957
复制相似问题