在ASP.NET中获取服务器的IP地址?

44

如何获取调用我的ASP.NET页面的服务器的IP地址? 我看到了一些关于Response对象的内容,但我对C#非常陌生。 非常感谢。

6个回答

69

这应该可以运行:

 //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

OR

 //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


11
顶部代码块获取运行代码的服务器 IP 地址。底部代码块获取发出请求的访问者的 IP 地址。 - Martin
1
它甚至显示了链接名称的差异,所以没有理由给我投反对票。 - TStamper
1
我取消了踩的操作,但我认为你应该编辑答案,以便清楚地说明哪个代码块执行什么操作。如果这两个网站在几个月内都从互联网上消失了,那么当有人查看这个答案时会发生什么? - Martin
3
Dns.Resolve已过时,应改用Dns.GetHostEntry(strHostName)。需要进行翻译的内容结束。 - Alberto León
1
Dns.Resolve(Dns.GetHostName())只会显示此计算机DNS名称的公开IP地址,如果您的计算机有多个IP地址、未在DNS中列出或使用某种网络地址转换,则可能无法得到您期望的结果。 - Michael Levy
显示剩余5条评论

42

Request.ServerVariables["LOCAL_ADDR"];

该代码可用于多宿主服务器,返回请求进入时的IP地址。


4
这也是最有效和稳定的方法。如果你使用 System.Net.Dns,那就错了。 - Shaun Wilson
2
更具体地说:System.Web.HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"]; - Chris Fremgen

14

上述方法较慢,因为它需要进行DNS调用(如果没有可用的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或IP以检查哪个是服务器? - Alberto León
我认为这个答案朝着正确的方向发展。服务器可以有多个网络适配器,而仅凭主机名可能会根据服务器设置给您错误的信息。其他答案在某些情况下可以并且将会起作用,但这个答案会让您更好地思考和理解您的环境。 - Nick

8
  //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();
  }

7

这适用于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;
}

0
以下截图来自Mkyong,展示了Google Chrome开发者控制台中的网络选项卡。在“请求标头”选项卡中,您可以看到如下所示的所有服务器变量列表:

enter image description here

以下是几行代码,用于获取访问您的应用程序的客户端的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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接