欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET 获取客户端IP方法

程序员文章站 2023-11-21 21:50:04
话不多说,请看代码: string requestclientipaddress = httpcontext.current.request.servervari...

话不多说,请看代码:

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 取出的是被分配的保留地址

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