46

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.

6 Answers 6

69

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

10
  • 11
    The top code block gets the IP address of the server that the code is running on. The bottom code block gets the IP address of the visitor who makes the request.
    – Martin
    Commented Mar 14, 2009 at 19:29
  • 1
    It even shows the difference in the names of the link.so that was no reason to downvote me
    – TStamper
    Commented Mar 14, 2009 at 19:47
  • 1
    I removed the downvote, but I think you should edit the answer to make it clear which code block does what. What happens if both those websites dissappear off the internet in a few months when someone is looking at this answer?
    – Martin
    Commented Mar 14, 2009 at 19:50
  • 3
    Dns.Resolve is obsolete you should use Dns.GetHostEntry(strHostName) instead Commented Nov 28, 2011 at 15:47
  • 1
    Doesn't Dns.Resolve(Dns.GetHostName()) only show you the publicly listed IP address for this machine's DNS name? This may not give the results you expect if you have multiple IP addresses on your machine or your machine is not listed in a DNS or you use some kind of network address translation. Commented Mar 29, 2013 at 13:43
42

Request.ServerVariables["LOCAL_ADDR"];

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

2
  • 4
    This is also the most efficient and stable method. If you're using System.Net.Dns you're doing it wrong.
    – wilson0x4d
    Commented Sep 18, 2013 at 19:40
  • 2
    More specifically: System.Web.HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"]; Commented May 11, 2016 at 15:14
14

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

2
  • And... with this code... how do you check which is the server that is running a site? I think... I have a production and a replication server, how do I store the ip or ips to check wich is the server? Commented Nov 30, 2011 at 10:46
  • I think this answer is headed in the right direction. Servers can have multiple network adapters, and host name alone can give you the wrong info depending on the server setup. The other answers can and will work in some situations, but this is the answer that makes you better think and understand your environment.
    – Nick
    Commented Jan 31, 2016 at 20:54
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

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;
}
0
0

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:

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.