What is Request.UserHostName actually returning and is it possible to convert it to something that I can get a host entry for?

httpcontext current request userhostname
request userhostaddress

I think I may be in a situation where the answer is this is not possible but in case not here goes...

I have written an ASP .NET MVC 3 application and I am using the Request.UserHostName property and then passing the value that that returns into Dns.GetHostEntry to find out all the possible IPs and the host name for the currently connected client, for example:

var clientAddress = Request.UserHostName;
var entry = Dns.GetHostEntry(clientAddress);

Generally that is fine except I have a case where I get a "host not found" SocketException from the Dns.GetHostEntry call.

The really odd thing is that the address that is returned from the Request.UserHostName property is not the public address or any of the private addresses. To prove this I ran this bit of code on the client machine in question...

var host = Dns.GetHostEntry(Dns.GetHostName());

foreach (var a in host.Aliases)
    Console.WriteLine("alias '{0}'", a);

foreach (var a in host.AddressList)
    Console.WriteLine("ip address '{0}'", a);

// ...from http://stackoverflow.com/a/2353177/1039947...
String direction = "";
WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
using (WebResponse response = request.GetResponse())
    using (var stream = new StreamReader(response.GetResponseStream()))
        direction = stream.ReadToEnd();

int first = direction.IndexOf("Address: ") + 9;
int last = direction.LastIndexOf("</body>");
direction = direction.Substring(first, last - first);

Console.WriteLine("Public IP: '{0}'", direction);

It prints three IP addresses (::1, one private and one public) but none of them are the address that is returned from Request.UserHostName.

If I pass in any of the addresses printed from the above test application into the Dns.GetHostEntry method I get a sensible value back.

So, is there any way that I could get from this strange IP address that is not the public nor any of the privates, to one where I could get the host entry for it without an exception (and what is this address)?

By the way, there is no X_FORWARD_FOR header or anything else that I may be able to identify the client with, as far as I can tell, in the HTTP message?

Background to the Question

So it was pointed out (thanks Damien) that if I explained why I am asking this perhaps someone can provide an alternative approach so here is some background...

I have a requirement that the administrator of the application should be allowed to specify in the configuration a single machine that is allowed to view the page - IP address or machine name - I can probably get the machine name requirement removed but even if they specify the IP address in the configuration it will still not match the IP address that is returned from the UserHostName property since they will use the IP address that is returned when they ping the machine name.

My thinking, therefore, was that if I take whatever is sent in the HTTP header and pass that into GetHostEntry then take all the possible results from that (all the IPs and the host name) and see if any of them match the configured value I could say "allow" otherwise "disallow" (I was going to remove the part of the host name before the first dot too, to cover that eventuality). That scheme has been blown out of the water by this situation I have where the IP address is not at all what I would expect.

The host name of the client is not normally known because it is not transmitted at the HTTP level. The server cannot know it. Look at the HTTP requests with Fiddler to see for yourself that there is not a lot of information available to the server (and the client can forge all request contents of course).

Use the UserHostAddress property to get the IP address. That is the most you can reliably find out. Once you have that you can try to reverse the IP to a host name but that is not always possible.

HttpRequest.UserHostName Property (System.Web), UserHostName Property. Definition C# Copy. public string UserHostName { get ; } member this. Write request information to the file with HTML encoding. sw. gethostbyaddr() - Get the Internet host name corresponding to a given IP address gethostbynamel() - Get a list of IPv4 addresses corresponding to a given Internet host name inet_pton() - Converts a human readable IP address to its packed in_addr representation

I have a more specific answer to your question. By examining the source code for HttpRequest.UserHostName here, I found that it maps to a IIS server variable named REMOTE_HOST which is described here. The property will return the IP adddress of the client, unless you have configured IIS in the way described, in which case IIS will do a reverse DNS lookup to attempt to return the name associated with the IP.

Sams Teach Yourself PHP in 24 Hours, pointer, you can use it as normal to read the file. request: GET /source/readthis. php HTTP/1.0 Host: p24.corrosive.co.uk: 9090 You can also Converting IP Addresses and Hostnames Even if your server does not provide you You can use this in conjunction with the function get host by add r( ) to get the user's hostname. One ASP.NET Web API related question that frequently comes up frequently is how to capture the raw request content to a simple parameter on a controller method. Turns out that's not as easy as it should be. In this post I discuss how to natively capture the raw request content and then create a [NakedBody] attribute that makes it easy capture the raw content in a string or byte[] parameter.

Make sure you read the Remarks section at Dns.GetHostEntry on the many cases it can (partially) fail:


The GetHostEntry method queries a DNS server for the IP address that is associated with a host name or IP address.

If an empty string is passed as the hostNameOrAddress argument, then this method returns the IPv4 and IPv6 addresses of the local host.

If the host name could not be found, the SocketException exception is returned with a value of 11001 (Windows Sockets error WSAHOST_NOT_FOUND). This exception can be returned if the DNS server does not respond. This exception can also be returned if the name is not an official host name or alias, or it cannot be found in the database(s) being queried.

The ArgumentException exception is also returned if the hostNameOrAddress parameter contains Any or IPv6Any.

The GetHostEntry method assumes that if an IP literal string is passed in the hostNameOrAddress parameter that the application wants an IPHostEntry instance returned with all of the properties set. These properties include the AddressList, Aliases, and HostName. As a result, the implementation of the GetHostEntry method exhibits the following behavior when an IP string literal is passed:

  1. The method tries to parse the address. If the hostNameOrAddress parameter contains a legal IP string literal, then the first phase succeeds.
  2. A reverse lookup using the IP address of the IP string literal is attempted to obtain the host name. This result is set as the HostName property.
  3. The host name from this reverse lookup is used again to obtain all the possible IP addresses associated with the name and set as the AddressList property.

For an IPv4 string literal, all three steps above may succeed. But it is possible for a stale DNS record for an IPv4 address that actually belongs to a different host to be returned. This may cause step #3 to fail and throw an exception (there is a DNS PTR record for the IPv4 address, but no DNS A record for the IPv4 address).

For IPv6, step #2 above may fail, since most IPv6 deployments do not register the reverse (PTR) record for an IPv6 address. So this method may return the string IPv6 literal as the fully-qualified domain (FQDN) host name in the HostName property.

The GetHostAddresses method has different behavior with respect to IP literals. If step #1 above succeeds (it successfully parses as an IP address), that address is immediately returned as the result. There is no attempt at a reverse lookup.

IPv6 addresses are filtered from the results of the GetHostEntry method if the local computer does not have IPv6 installed. As a result, it is possible to get back an empty IPHostEntry instance if only IPv6 results where available for the hostNameOrAddress.parameter.

The Aliases property of the IPHostEntry instance returned is not populated by this method and will always be empty.

Dns.GetHostEntry Method (System.Net), Resolves a host name or IP address to an instance. As a result, it is possible to get back an empty IPHostEntry instance if only IPv6 This exception can be returned if the DNS server does not respond. But it is possible for a stale DNS record for an IPv4 address that actually belongs to a different host to be returned. All the methods like GET, POST can work, with if any headers and body, but there is a web server is involved, it could be point of failure, so scalability and availability will be key If you are on AWS , Route 53 -> API Gateway is possible though custom domains, internally using the Cloud front

Temporarily bypassing DNS by modifying the Windows hosts file , If you have a Windows machine, temporarily adding a line to the hosts file is the easiest way to test a DNS change before actually making it live. Of course, you probably want to resolve DNS to something else, like the cloud load balancing S) but leave the notepad file open so you can return it to normal in a few minutes. In responses, a Content-Type header tells the client what the content type of the returned content actually is. Browsers will do MIME sniffing in some cases and will not necessarily follow the value of this header; to prevent this behavior, the header X-Content-Type-Options can be set to nosniff .

SSH Tricks, ssh -p 2222 user@hostname Here's something really powerful. vim ~/.ssh/ config Host somealias HostName example.com Port 2222 User So, there's a few example entries you might find in the ~/.ssh/config file (you can have as many Assuming we have SSH access to the remote server, we can get� In your initial response, you stated that it would be possible to convert a database that was created as US7ASCII to the WE8ISO8859P1. When I created one of my databases, I made the mistake of creating it as US7ASCII, instead of the other one, so the database cannot store the French accented characters that I need.

8.3. Setting the Hostname and Configuring the Name Service, The purpose of assigning names to IP numbers is to make them easier for people to You can change it in the /etc/hosts file; simply write a complete name for the To access DNS information, a DNS server must be available to relay requests. If there is no name server on the local network, it is still possible to establish a� To get the value of the selected item of a listbox then use the following. For Single Column ListBox: ListBox1.List(ListBox1.ListIndex) For Multi Column ListBox: ListBox1.Column(column_number, ListBox1.ListIndex) This avoids looping and is extremely more efficient.

  • All you can generally tell (in HTTP) is an IP address that the request purports to come from (this may actually be a firewall or proxy address, X_FORWARD_FOR is a nicety but not actually required). I have a feeling we'd do much better if we knew the overall problem to be solved here (as in, I'm sure your final goal isn't "obtain a host entry for the client" - that sounds like part of a "solution" - one that isn't going to work)
  • @Damien_The_Unbeliever Thanks - I have added some background
  • Unless you're in a tightly constrained network, there's no way to achieve the requirement that's been asked for. Is this an intranet site or an internet facing one?
  • @Damien_The_Unbeliever an intranet application - not internet facing
  • possible duplicate of How can I read the client's machine/computer name from the browser?
  • That's correct. Relying on .NET to convert the IP to a host only to then convert that host back to the IP is failure-prone and not useful.
  • And there might be zero or multiple host names for any given IP! And there might be many IPs for any given host. It is m:n. The mapping between IP and host name is not well defined.
  • Thanks - that was what I concluded before asking the question too - just wondered if there was some other way (perhaps lower in the stack) that I could get hold of the IP address that I would be able to reliably match against but I guess not
  • Certainly not because the HTTP request does not contain the host name, neither do the lower layers.
  • The above link that describes the REMOTE_HOST user variable is rotten. A web search for "configure iis reverse lookup remote_host" produces some useful results, but it might be that IIS no longer supports the reverse-lookup option.