7

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

3 Answers 3

10

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.

Sign up to request clarification or add additional context in comments.

11 Comments

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.
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%.
Why not include that detail in your answer?
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.
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.
|
3

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"];
}

1 Comment

This does not work for me in ASP.NET Core Razor Pages in .NET 7, running on an IIS 10 in an intranet environment with Windows Authentication enforced.
-2

Try this

this.Request.Host.Value

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.