6

Couldn't find the UserHostName property on HttpRequest. And nothing about it in any forums. Has this been dropped? Any idea?

My3
  • 121
  • 3
  • 8

3 Answers3

9

The accepted answer is not correct. someHttpContext.Request.Host corresponds to the Host header used in the HTTP request. For example: fetching http://example.org/test will result in example.org.

UserHostName used to return the DNS hostname of the client IP address making the request (which is someHttpContext.Connection.RemoteIpAddress). It looks as though the only recourse is to manually resolve the DNS hostname from the IP address with e.g. Dns.GetHostEntry.

Jesper
  • 7,477
  • 4
  • 40
  • 57
  • [this](https://stackoverflow.com/questions/27132908/userhostaddress-in-asp-net-core) answer says that the `someHttpContext.Connection.RemoteIpAddress` refers to the `UserHostAddress` and not to the `UserHostName`. – manymanymore Feb 26 '21 at 09:46
  • 1
    Yes, exactly, and that's what my answer says too. It is the IP address that you have to manually DNS resolve to get the `UserHostName` equivalent. – Jesper Feb 26 '21 at 09:51
  • So, again. How can I get the `UserHostName`? – manymanymore Feb 26 '21 at 10:02
  • By performing DNS resolution on `RemoteIpAddress` by `Dns.GetHostEntry` and the like. – Jesper Feb 26 '21 at 14:59
  • Could you, please, include an example of getting the `UserHostName`? Because it seems you are just trying to guess that it is possible, while you are not sure 100%. – manymanymore Mar 09 '21 at 06:44
  • No, I'm saying that you would use the [GetHostEntry](https://learn.microsoft.com/en-us/dotnet/api/system.net.dns.gethostentry) method on the Dns class, calling it with the remote IP address and then looking through the result for the name. Calling this method is trivial and straightforward. – Jesper Mar 09 '21 at 09:50
  • Why not include that detail in your answer? – manymanymore Mar 09 '21 at 10:29
  • Including it in the answer is fair (and I have now done so), but it sounded like you wanted me to provide sample code for a simple method call. – Jesper Mar 09 '21 at 10:33
  • Adding an example of a method call would be a lot more beneficial than describing the same code. Yes, I want you to provide the sample code. It is easier to read the code than to read a text which explains a non-existent code. – manymanymore Mar 09 '21 at 15:07
  • 1
    Fair enough, but I don't consider the method cryptic enough to need that level of explanation, and I don't consider just mentioning which method and leaving the actual code as an exercise to the reader to be unhelpful or rude. If someone does require that level of assistance, the documentation I'm linking to actually has it, and a lot more detail that I also don't see the need to duplicate here. – Jesper Mar 09 '21 at 18:28
2

In "old" ASP.NET this was relying on IIS, that puts user hostname into "REMOTE_HOST" server variable.

Since ASP.NET Core is cross platform, it does not have this feature.

But if you run on Windows, and on IIS, you can still access it via HttpContext

var svf = httpContext.Features.Get<IServerVariablesFeature>();
if (svf == null)
{
    //we're on linux, sorry, no luck
}
else
{
    var hosteName = svf["REMOTE_HOST"];
}
Alex from Jitbit
  • 53,710
  • 19
  • 160
  • 149
-1

Try this

this.Request.Host.Value