如何通过ASP.NET获取访问者的客户端IP地址?

更新于
2026-09-24 00:06:50
12阅读来源:SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计172个文字,预计阅读时间需要1分钟。

如何通过ASP.NET获取访问者的客户端IP地址?

csharp// 获取客户端IP地址string clientIpAddress=HttpContext.Current.Request.ServerVariables[HTTP_X_FORWARDED_FOR];if (string.IsNullOrEmpty(clientIpAddress)){ clientIpAddress=HttpContext.Current.Request.Server;}

话不多说,请看代码:

string requestClientIpAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (string.IsNullOrEmpty(requestClientIpAddress)) requestClientIpAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; if (string.IsNullOrEmpty(requestClientIpAddress)) requestClientIpAddress = HttpContext.Current.Request.UserHostAddress;

经过测试 存在负载均衡的时候 ,HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] 取出的是真实的客户端 IP地址 ,而HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] 和 HttpContext.Current.Request.UserHostAddress 取出的是被分配的保留地址

如何通过ASP.NET获取访问者的客户端IP地址?

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持易盾网络!

本文共计172个文字,预计阅读时间需要1分钟。

如何通过ASP.NET获取访问者的客户端IP地址?

csharp// 获取客户端IP地址string clientIpAddress=HttpContext.Current.Request.ServerVariables[HTTP_X_FORWARDED_FOR];if (string.IsNullOrEmpty(clientIpAddress)){ clientIpAddress=HttpContext.Current.Request.Server;}

话不多说,请看代码:

string requestClientIpAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (string.IsNullOrEmpty(requestClientIpAddress)) requestClientIpAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; if (string.IsNullOrEmpty(requestClientIpAddress)) requestClientIpAddress = HttpContext.Current.Request.UserHostAddress;

经过测试 存在负载均衡的时候 ,HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] 取出的是真实的客户端 IP地址 ,而HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] 和 HttpContext.Current.Request.UserHostAddress 取出的是被分配的保留地址

如何通过ASP.NET获取访问者的客户端IP地址?

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持易盾网络!