简体   繁体   English

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

[英]Getting the IP address of server in ASP.NET?

How do I get the IP address of the server that calls my ASP.NET page? 如何获取调用ASP.NET页面的服务器的IP地址? I have seen stuff about a Response object, but am very new at c#. 我见过有关Response对象的内容,但在c#中我是新手。 Thanks a ton. 万分感谢。

This should work: 这应该工作:

 //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 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 http://www.geekpedia.com/KB32_How-do-I-get-the-visitors-IP-address.html

Request.ServerVariables["LOCAL_ADDR"];

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

The above is slow as it requires a DNS call (and will obviously not work if one is not available). 以上是缓慢的,因为它需要DNS调用(如果一个不可用,显然不会工作)。 You can use the code below to get a map of the current pc's local IPV4 addresses with their corresponding subnet mask: 您可以使用下面的代码获取当前pc的本地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;
}

warning: this is not implemented in Mono yet 警告:尚未在Mono中实现

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

This will work for IPv4: 这适用于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;
}

The below snap is taken from Mkyong to show the networks tab inside developers console in google chrome.Inside "Request Headers" tab you could see a list of all server variables as shown below: 下面的快照取自Mkyong ,在google chrome中显示开发者控制台内的网络选项卡。在“请求标题”选项卡中,您可以看到所有服务器变量的列表,如下所示:

在此输入图像描述

Below are few lines of code which gets the ipaddress of the client which hits your application 下面是几行代码,它们获取了客户端的ipaddress,它可以满足你的应用程序

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM