如何通过ASP.NET获取客户端IP地址的具体实现方法?

2026-03-31 08:121阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

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

如何通过ASP.NET获取客户端IP地址的具体实现方法?

获取客户端IP地址的代码可以简化如下:

csharpstring requestClientIpAddress=HttpContext.Current.Request.ServerVariables[HTTP_X_FORWARDED_FOR];if (string.IsNullOrEmpty(requestClientIpAddress)) requestClientIpAddress=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地址的具体实现方法?

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

如何通过ASP.NET获取客户端IP地址的具体实现方法?

获取客户端IP地址的代码可以简化如下:

csharpstring requestClientIpAddress=HttpContext.Current.Request.ServerVariables[HTTP_X_FORWARDED_FOR];if (string.IsNullOrEmpty(requestClientIpAddress)) requestClientIpAddress=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地址的具体实现方法?