Getting the IP address of server in ASP.NET?

ぐ巨炮叔叔 提交于 2019-12-28 01:57:06

问题


How do I get the IP address of the server that calls my ASP.NET page? I have seen stuff about a Response object, but am very new at c#. Thanks a ton.


回答1:


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

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




回答2:


Request.ServerVariables["LOCAL_ADDR"];

This gives the IP the request came in on for multi-homed servers




回答3:


The above is slow as it requires a DNS call (and will obviously not work if one is not available). You can use the code below to get a map of the current pc's local IPV4 addresses with their corresponding subnet mask:

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




回答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:


This will work for 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:


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:

Below are few lines of code which gets the ipaddress of the client which hits your application

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


来源:https://stackoverflow.com/questions/646525/getting-the-ip-address-of-server-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!