28

Is there a 1 line method to get the IP Address of the server?

Thanks

Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
JL.
  • 78,954
  • 126
  • 311
  • 459
  • What you mean "server" - ASP.Net processing machine or server-side firewall/gate/proxy – Dewfy Aug 28 '09 at 08:23
  • 2
    You need to take into account that there can be many IP addresses assigned to your server. – UserControl Aug 28 '09 at 08:28
  • Possible duplicate of http://stackoverflow.com/q/646525/292060, even though this has a better selected answer. – goodeye Dec 20 '14 at 04:49
  • I would love to see how to proceed when you have to rely on a `HttpRequestMessage` object instead of the classic `HttpRequest`. – SandRock Sep 24 '16 at 15:50
  • @SandRock - you could take a look [here](https://gist.github.com/MikeJansen/2653453) which looks for the `MS_HttpContext` property and then casts to an `HttpContextBase` which would allow access to the `ServerVariables` property. Depending on where you're doing this, you may also be able to grab the HttpContext directly and query that as `HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"]` (sorry for the late reply ;)) – Zhaph - Ben Duguid Jun 20 '17 at 11:02

4 Answers4

58
Request.ServerVariables["LOCAL_ADDR"];

From the docs:

Returns the server address on which the request came in. This is important on computers where there can be multiple IP addresses bound to the computer, and you want to find out which address the request used.

This is distinct from the Remote addresses which relate to the client machine.

Zhaph - Ben Duguid
  • 26,785
  • 5
  • 80
  • 117
  • Yep, lots of handing things hiding in the ServerVariables collection. – Zhaph - Ben Duguid Aug 28 '09 at 09:13
  • 2
    Perfect answer - from old ASP Classic days , I should have remembered this one :) – JL. Aug 31 '09 at 05:09
  • Slight glitch: When hitting the server from a browser on the server, it resolves to 127.0.0.1. Otherwise, love it. – Allbite Sep 15 '11 at 17:31
  • 1
    @Allbite - That possibly depends on how the DNS on the server is configured, or how you request the site - for example if there's a HOSTS entry pointing the domain to 127.0.0.1 to ensure that you browse just the local server (in a load balanced environment for example), or you request the site as LocalHost, then yes, you'll definitely get 127.0.0.1 back. I've had other sites that reported the correct IP address for the browser when they've gone via a proper DNS lookup. – Zhaph - Ben Duguid Sep 16 '11 at 12:28
  • 1
    This shows the address of the server that sent the request NOT The server that is hosting the website – Talha May 25 '16 at 13:25
  • @Talha - Nope, `LOCAL_ADDR` returns the network address associated with the hosted instance of the site - `REMOTE_ADDR` returns the address of the requesting client: "`REMOTE_ADDR` The IP address of the remote host that is making the request." – Zhaph - Ben Duguid May 26 '16 at 14:53
6

From searching the net I found following code: (I couldn't find a single line method there)

string myHost = System.Net.Dns.GetHostName();

// Show the hostname 

MessageBox.Show(myHost);

// Get the IP from the host name

string myIP = System.Net.Dns.GetHostEntry(myHost).AddressList[index].ToString();

// Show the IP 

MessageBox.Show(myIP);

-> where index is the index of your ip address host (ie. network connection).

Code from: http://www.geekpedia.com/tutorial149_Get-the-IP-address-in-a-Windows-application.html

Faizan S.
  • 8,634
  • 8
  • 34
  • 63
  • 1
    "where index is the index of your ip address host (ie. network connection)." - What does this mean? That link no longer works – Nigel Fds Oct 12 '18 at 01:33
2

As other(s) have posted, System.Net.Dns.GetHostEntry is the way to go. When you access the AddressList property, you'll want to take the AddressFamily property into account, as it could return both IPv4 AND IPv6 results.

Mark Carpenter
  • 17,445
  • 22
  • 96
  • 149
0

This method will return your machine public IP address when run this code on your PC and when you deploy your application on server will return Server IP address.

public static string Getpublicip()
    {
        try
        {
            string externalIP = "";
            var request = (HttpWebRequest)WebRequest.Create("http://icanhazip.com.ipaddress.com/");
            var response = (HttpWebResponse)request.GetResponse();
            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

            externalIP = new WebClient().DownloadString("http://icanhazip.com");
            return externalIP;

        }
        catch (Exception e)
        {

            return "null";
        }

    }
TAHA SULTAN TEMURI
  • 4,031
  • 2
  • 40
  • 66