title: What is Request.UserHostName actually returning and is it possible to convert it to something that I can get a host entry for?
body: <p>I think I may be in a situation where the answer is <code>this is not possible</code> but in case not here goes...</p>
  
  <p>I have written an <code>ASP .NET MVC 3</code> application and I am  using the <a href="http://msdn.microsoft.com/en-us/library/system.web.httprequestbase.userhostname.aspx" rel="noreferrer">Request.UserHostName</a> property and then passing the value that that returns into <a href="http://msdn.microsoft.com/en-us/library/ms143998%28v=vs.80%29.aspx" rel="noreferrer">Dns.GetHostEntry</a> to find out all the possible IPs and the host name for the currently connected client, for example:</p>
  
  <pre><code>var clientAddress = Request.UserHostName;
  var entry = Dns.GetHostEntry(clientAddress);
  </code></pre>
  
  <p>Generally that is fine except I have a case where I get a "host not found" <a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.socketexception.aspx" rel="noreferrer">SocketException</a> from the <a href="http://msdn.microsoft.com/en-us/library/ms143998%28v=vs.80%29.aspx" rel="noreferrer">Dns.GetHostEntry</a> call.  </p>
  
  <p>The really odd thing is that the address that is returned from the <a href="http://msdn.microsoft.com/en-us/library/system.web.httprequestbase.userhostname.aspx" rel="noreferrer">Request.UserHostName</a> 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...</p>
  
  <pre><code>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("&lt;/body&gt;");
  direction = direction.Substring(first, last - first);
  
  Console.WriteLine("Public IP: '{0}'", direction);
  </code></pre>
  
  <p>It prints three IP addresses (::1, one private and one public) but none of them are the address that is returned from <a href="http://msdn.microsoft.com/en-us/library/system.web.httprequestbase.userhostname.aspx" rel="noreferrer">Request.UserHostName</a>.</p>
  
  <p>If I pass in any of the addresses  printed from the above test application into the <a href="http://msdn.microsoft.com/en-us/library/ms143998%28v=vs.80%29.aspx" rel="noreferrer">Dns.GetHostEntry</a> method I get a sensible value back.</p>
  
  <p>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)?</p>
  
  <p>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?</p>
  
  <h2>Background to the Question</h2>
  
  <p>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...</p>
  
  <p>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.</p>
  
  <p>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.</p>