【问题标题】:Getting the IP address of server in ASP.NET?在 ASP.NET 中获取服务器的 IP 地址?
【发布时间】:2010-10-13 09:21:25
【问题描述】:

如何获取调用我的 ASP.NET 页面的服务器的 IP 地址?我见过关于 Response 对象的东西,但在 c# 中我很新。非常感谢。

【问题讨论】:

    标签: c# asp.net dns referrer


    【解决方案1】:

    这应该有效:

     //this gets the ip address of the server pc
    
      public string GetIPAddress()
      {
         IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); // `Dns.Resolve()` method is deprecated.
         IPAddress ipAddress = ipHostInfo.AddressList[0];
    
         return ipAddress.ToString();
      }
    

    http://wec-library.blogspot.com/2008/03/gets-ip-address-of-server-pc-using-c.html

     //while this gets the ip address of the visitor making the call
      HttpContext.Current.Request.UserHostAddress;
    

    http://www.geekpedia.com/KB32_How-do-I-get-the-visitors-IP-address.html

    【讨论】:

    • 顶部代码块获取运行代码的服务器的 IP 地址。底部代码块获取发出请求的访问者的 IP 地址。
    • 它甚至显示了链接名称的不同。所以没有理由拒绝我
    • 我删除了反对票,但我认为您应该编辑答案以明确哪个代码块做什么。如果有人在查看此答案时,如果这两个网站在几个月内从互联网上消失,会发生什么?
    • Dns.Resolve 已过时,您应该改用 Dns.GetHostEntry(strHostName)
    • 难道 Dns.Resolve(Dns.GetHostName()) 不只向您显示这台机器的 DNS 名称的公开列出的 IP 地址吗?如果您的计算机上有多个 IP 地址,或者您的计算机未在 DNS 中列出,或者您使用某种网络地址转换,这可能不会给出您期望的结果。
    【解决方案2】:

    Request.ServerVariables["LOCAL_ADDR"];

    这为多宿主服务器提供了请求进入的 IP

    【讨论】:

    • 这也是最高效稳定的方法。如果你使用System.Net.Dns,那你就错了。
    • 更具体地说:System.Web.HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"];
    【解决方案3】:

    上面的方法很慢,因为它需要一个 DNS 调用(如果一个不可用,显然不会工作)。您可以使用下面的代码获取当前电脑的本地 IPV4 地址及其对应的子网掩码的映射:

    public static Dictionary<IPAddress, IPAddress> GetAllNetworkInterfaceIpv4Addresses()
    {
        var map = new Dictionary<IPAddress, IPAddress>();
    
        foreach (var ni in NetworkInterface.GetAllNetworkInterfaces())
        {
            foreach (var uipi in ni.GetIPProperties().UnicastAddresses)
            {
                if (uipi.Address.AddressFamily != AddressFamily.InterNetwork) continue;
    
                if (uipi.IPv4Mask == null) continue; //ignore 127.0.0.1
                map[uipi.Address] = uipi.IPv4Mask;
            }
        }
        return map;
    }
    

    警告:这在 Mono 中尚未实现

    【讨论】:

    • 而且...使用此代码...您如何检查运行站点的服务器是哪个?我想...我有一个生产服务器和一个复制服务器,我如何存储 ip 或 ips 以检查服务器是哪一个?
    • 我认为这个答案正朝着正确的方向发展。服务器可以有多个网络适配器,根据服务器设置,仅主机名可能会给您错误的信息。其他答案在某些情况下可以并且将会起作用,但这是让您更好地思考和了解您的环境的答案。
    【解决方案4】:
      //this gets the ip address of the server pc
      public string GetIPAddress()
      {
         string strHostName = System.Net.Dns.GetHostName();
         //IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); <-- Obsolete
         IPHostEntry ipHostInfo = Dns.GetHostEntry(strHostName);
         IPAddress ipAddress = ipHostInfo.AddressList[0];
    
         return ipAddress.ToString();
      }
    

    【讨论】:

      【解决方案5】:

      这适用于 IPv4:

      public static string GetServerIP()
      {            
          IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
      
          foreach (IPAddress address in ipHostInfo.AddressList)
          {
              if (address.AddressFamily == AddressFamily.InterNetwork)
                  return address.ToString();
          }
      
          return string.Empty;
      }
      

      【讨论】:

        【解决方案6】:

        下面的快照取自Mkyong,用于在谷歌浏览器中显示开发者控制台中的网络选项卡。在“请求标头”选项卡中,您可以看到所有服务器变量的列表,如下所示:

        下面是几行代码,可以获取访问您的应用程序的客户端的 IP 地址

        //gets the ipaddress of the machine hitting your production server              
        string ipAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; 
        
        if (ipAddress == "" || ipAddress == null)  
        {                                     
          //gets the ipaddress of your local server(localhost) during development phase                                                                         
          ipAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];              
        }
        
        //Output:                                                                           
        For production server - 122.169.106.247 (random)
        For localhost         - ::1
        

        【讨论】:

          猜你喜欢
          • 2010-11-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-18
          • 2010-09-22
          • 1970-01-01
          • 2016-12-04
          • 1970-01-01
          相关资源
          最近更新 更多