本文主要介绍了ASP.NET获取客户端IP方法,具有很好的参考价值,下面跟着小编一起来看下吧
话不多说,请看代码:
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方法的详细内容,更多请关注0133技术站其它相关文章!