DIRECT replacement of UserHostAddress in ASP.NET Core?

自闭症网瘾萝莉.ら 提交于 2019-12-14 01:51:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!