【问题标题】:How to retrieve IP of client which has make request to C# webservice如何检索已向 C# webservice 发出请求的客户端的 IP
【发布时间】:2011-01-07 09:06:14
【问题描述】:

我已经创建了 C# Web 服务。我不希望每个人都调用我的网络服务。我认为获得 ip 我可以保护执行一些方法。谁能告诉我用 ip 或其他方式保护 Webservice 的方法

【问题讨论】:

标签: c# web-services


【解决方案1】:

在网络服务中是:

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")}
};

【讨论】:

  • 它返回类似于 ::1 而不是客户端 ip 的想法
  • 找不到 _privateIps 变量
  • 抱歉忘记复制了!我最后加了。
【解决方案2】:

从请求对象Request.UserHostAddress中获取IP地址

然后测试它是否等于您允许的 IP 地址,如果它是服务内容,如果不返回 http 403 状态代码(如果您想提供其他信息,IIS 拒绝 IP 地址的 403.6)

【讨论】:

    猜你喜欢
    • 2018-08-21
    • 2020-01-18
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 2011-03-07
    • 2014-10-24
    • 2019-01-13
    • 2012-09-01
    相关资源
    最近更新 更多