简体   繁体   English

ASP.Net Core检测真实客户端IP

[英]ASP.Net Core Detect real client IP

I've got an ASP.NET Core app hosting on IIS, the server with which is behind the router. 我在IIS上托管了一个ASP.NET Core应用程序,该服务器位于路由器后面。 I need to detect real clients IPs. 我需要检测真实的客户端IP。 From the local network context.HttpContext.Connection.RemoteIpAddress returns correct IP, but from the outside it is always 127.0.0.1 . 从本地网络context.HttpContext.Connection.RemoteIpAddress返回正确的IP,但从外部始终为127.0.0.1

Also I tried to look at X-Original-For header which locally gives 127.0.0.1 and nothing form the outside. 我还尝试查看X-Original-For标头,它在本地提供127.0.0.1 ,而外部没有任何形式。

I tried UseForwardedHeaders ( https://stackoverflow.com/a/41450563/4801505 ) middleware but as I got it the middleware is already used by UseIISIntegration ( https://github.com/aspnet/Docs/issues/2384 ) so there is no sense using it twice. 我尝试了UseForwardedHeadershttps://stackoverflow.com/a/41450563/4801505 )中间件,但据我UseIISIntegration ,中间件已被UseIISIntegrationhttps://github.com/aspnet/Docs/issues/2384 )使用。两次使用是没有意义的。

So, can I achieve the thing at all in my circumstances? 那么,在我所处的环境中,我能完全实现目标吗? Maybe some IIS config required? 也许需要一些IIS配置?

Use the external link to get ip address. 使用外部链接获取IP地址。

I end up using 我最终使用

 var httpClient = new HttpClient();
        var ip = await httpClient.GetStringAsync("https://api.ipify.org");

If you are using any language other than C# refer to website ipify 如果您使用的是C#以外的其他语言,请访问ipify网站

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM