简体   繁体   English

当用户运行托管 IIS 链接时,如何在 asp.net 中获取客户端 IP 地址

[英]How to get the client IP address in asp.net when user runs hosted IIS link

I hosted my code in IIS,I want to get the user Ip when any user logged into my application using my hosted link.我在 IIS 中托管了我的代码,当任何用户使用我的托管链接登录我的应用程序时,我想获取用户 Ip。 For example if 10 different users were accessed my link from 10 different systems means,I want to get all those 10 different IP address in asp.net c#.例如,如果 10 个不同的用户从 10 个不同的系统访问我的链接,我想在 asp.net c# 中获取所有这 10 个不同的 IP 地址。 Is that same code will bring both server and IP address.I'm using Asp.net 4.5 -c#相同的代码是否会同时带来服务器和 IP 地址。我正在使用 Asp.net 4.5 -c#

Probably one of:可能是其中之一:

HttpContext.Current.Request.ServerVariables.Item("REMOTE_HOST")
HttpContext.Current.Request.ServerVariables.Item("REMOTE_ADDR")

Or...或者...

HttpContext.Current.Request.ServerVariables.Item("X-Forwarded-For")

...if your server is behind a proxy. ...如果您的服务器位于代理后面。 Be aware that the last one can be faked quite easily (and IPs are fakable/alterable too), so don't rely on it for anything related to security请注意,最后一个很容易被伪造(并且 IP 也是可伪造/可更改的),所以不要依赖它来处理与安全相关的任何事情

You can get client IP by this code:您可以通过以下代码获取客户端 IP:

HttpContext.Current.Request.UserHostAddress

you can get your IP address like this:你可以像这样得到你的 IP 地址:

var remoteIpAddress = request.HttpContext.Connection.RemoteIpAddress;

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

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