44

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.

शेखर
  • 17,412
  • 13
  • 61
  • 117
Jamison Dance
  • 19,896
  • 25
  • 97
  • 99

6 Answers6

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

Ganesh Jadhav
  • 2,830
  • 1
  • 20
  • 32
TStamper
  • 30,098
  • 10
  • 66
  • 73
  • 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 Mar 14 '09 at 19:29
  • The reason I put those two different request up there is for the question. That is why I put two links to go in detail for both, so the understanding was clarified – TStamper Mar 14 '09 at 19:45
  • 1
    It even shows the difference in the names of the link.so that was no reason to downvote me – TStamper Mar 14 '09 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 Mar 14 '09 at 19:50
  • that is true. Changes have been made – TStamper Mar 14 '09 at 20:00
  • 3
    Dns.Resolve is obsolete you should use Dns.GetHostEntry(strHostName) instead – Alberto León Nov 28 '11 at 15:47
  • this is the MSDN article I used to see Dns.Resolve obsolete [link](http://stackoverflow.com/questions/646525/getting-the-ip-address-of-server-in-asp-net/5537827#5537827) – Alberto León Nov 30 '11 at 10:37
  • It's best to test that AddressList[0] is the IP type you want e.g. IPv4 (address.AddressFamily == AddressFamily.InterNetwork) or IPv6 (AddressFamily.InterNetworkV6). IPv6 addresses don't make good file names. Ask me how I know! – James McLachlan Jul 26 '12 at 19:26
  • 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. – Michael Levy Mar 29 '13 at 13:43
  • This gave me whats looks like an IPv6 address - I wanted the IP v4 address - the solution below that uses Request.ServerVariables["LOCAL_ADDR"]; worked for me. – john blair Dec 29 '19 at 12:27
42

Request.ServerVariables["LOCAL_ADDR"];

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

Grhm
  • 6,726
  • 4
  • 40
  • 64
MrPurpleStreak
  • 1,477
  • 3
  • 17
  • 28
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

mythz
  • 141,670
  • 29
  • 246
  • 390
  • 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? – Alberto León Nov 30 '11 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 Jan 31 '16 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();
  }
Alberto León
  • 2,879
  • 2
  • 25
  • 24
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;
}
André Voltolini
  • 426
  • 8
  • 14
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
Tahir77667
  • 2,290
  • 20
  • 16