问题
We discovered that Request.UserHostAddress
is not available in ASP.NET Core (1.0 RC1), as well as Request.ServerVariables["REMOTE_ADDR"]
, which returns essentially the same.
Now we found HttpContext.Connection.RemoteIpAddress
or HttpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress
to be the replacement, but is it really a 1:1 replacement for the functionality?
What I mean: Request.ServerVariables["REMOTE_ADDR"]
only returns the direct IP address of a caller, not a forwarded client address (therefore you have to get "X-FORWARDED-FOR"
). And that's what we need in our scenario: the direct caller address, not the forwarded one. But we couldn't find any info if HttpContext.Connection.RemoteIpAddress
in ASP.NET Core is the right choice for us (and if not, what's the alternative?).
回答1:
You only get X-Forwarded-For replacing it when you have plumbed that middleware into the pipeline. So to enable that use app.UseOverrideHeaders()
.
Without it RemoteIpAddress is going to be 127.0.0.1 in most configurations, because you will have IIS in front of Kestrel.
来源:https://stackoverflow.com/questions/35794237/direct-replacement-of-userhostaddress-in-asp-net-core