使用C#的客户端IP

发布于 2024-10-09 16:29:19 字数 103 浏览 0 评论 0原文

如何在 C# 中获取客户端计算机的 IP 地址? 我想为我的在线应用程序保留一个日志寄存器,并保留日志系统的IP地址,我想获取客户端的IP地址......

提前谢谢......

How can i get IP address of client machine in C#.?
I want to keep a log register for my online application and to keep IP address of logging system i want to get the IP address of client....

Advance Thanks...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对你再特殊 2024-10-16 16:29:19
    String clientIP = 
(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]==null)?
HttpContext.Current.Request.UserHostAddress:
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    String clientIP = 
(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]==null)?
HttpContext.Current.Request.UserHostAddress:
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
笑叹一世浮沉 2024-10-16 16:29:19
HttpContext.Current.Request.UserHostAddress

这并不试图考虑代理。为此,您可以使用 Request.ServerVariables["HTTP_X_FORWARDED_FOR"]。但是,请确保您不会盲目相信它,因为它可能是伪造的。最好保留一份您信任的 IP 白名单。

HttpContext.Current.Request.UserHostAddress

This doesn't attempt to take into account proxies. For that, you can use Request.ServerVariables["HTTP_X_FORWARDED_FOR"]. However, make sure you're not trusting that blindly, since it could be forged. It's better to keep a whitelist of IPs for which you trust it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文