当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET获取真正的客户端IP地址的6种方法

ASP.NET获取真正的客户端IP地址的6种方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

巴马火麻油,49vvv,绝对仕途

在asp中使用

request.servervariables("remote_addr") 来取得客户端的ip地址,但如果客户端是使用代理服务器来访问,那取到的就是代理服务器的ip地址,而不是真正的客户端ip地址。

要想透过代理服务器取得客户端的真实ip地址,就要使用 request.servervariables("http_x_forwarded_for") 来读取。

不过要注意的事,并不是每个代理服务器都能用 request.servervariables("http_x_forwarded_for") 来读取客户端的真实 ip,有些用此方法读取到的仍然是代理服务器的ip。

还有一点需要注意的是:如果客户端没有通过代理服务器来访问,那么用 request.servervariables ("http_x_forwarded_for") 取到的值将是空的。因此,如果要在程序中使用此方法,可以这样处理:
......
userip = request.servervariables("http_x_forwarded_for")
if userip = "" then userip = request.servervariables("remote_addr")
......

服务端
//方法一
httpcontext.current.request.userhostaddress;
//方法二
httpcontext.current.request.servervariables["remote_addr"];
//方法三
string strhostname = system.net.dns.gethostname();
string clientipaddress = system.net.dns.gethostaddresses(strhostname).getvalue(0).tostring();
//方法四(无视代理)
httpcontext.current.request.servervariables["http_x_forwarded_for"];
客户端
//方法五
var ip = '<!--#echo var="remote_addr"-->';
alert("your ip address is "+ip);
//方法六(无视代理)
复制代码 代码如下:

function getlocalipaddress()
{
var obj = null;
var rslt = "";
try
{
obj = new activexobject("rcbdyctl.setting");
rslt = obj.getipaddress;
obj = null;
}
catch(e)
{
//
}
return rslt;
}

22日添加:
来自印度的mct maulik patel提供了一种服务端的解决方案,很好:
复制代码 代码如下:

if(context.request.servervariables["http_via"]!=null) // using proxy
{
ip=context.request.servervariables["http_x_forwarded_for"].tostring(); // return real client ip.
}
else// not using proxy or can't get the client ip
{
ip=context.request.servervariables["remote_addr"].tostring(); //while it can't get the client ip, it will return proxy ip.
}

备注
1. 有些代理是不会发给我们真实ip地址的
2. 有些客户端会因为“header_access deny”的安全设置而不发给我们ip

servervariables变量说明

servervariables参数
response.write(request.servervariables("varname"))
'varname就是需测的数据

all_http
客户端发送的所有http标头,他的结果都有前缀http_。

all_raw
客户端发送的所有http标头,其结果和客户端发送时一样,没有前缀http_

appl_md_path
应用程序的元数据库路径。

appl_physical_path
与应用程序元数据库路径相应的物理路径。

auth_password
当使用基本验证模式时,客户在密码对话框中输入的密码。

auth_type
这是用户访问受保护的脚本时,服务器用于检验用户的验证方法。

auth_user
代验证的用户名。

cert_cookie
唯一的客户证书id号。

cert_flag
客户证书标志,如有客户端证书,则bit0为0。如果客户端证书验证无效,bit1被设置为1。

cert_issuer
用户证书中的发行者字段。

cert_keysize
安全套接字层连接关键字的位数,如128。

cert_secretkeysize
服务器验证私人关键字的位数。如1024。

cert_serialnumber
客户证书的序列号字段。

cert_server_issuer
服务器证书的发行者字段

cert_server_subject
服务器证书的主题字段。

cert_subject
客户端证书的主题字段。

content_length
客户端发出内容的长度。

content_type
客户发送的form内容或http put的数据类型。

gateway_interface
服务器使用的网关界面。

https
如果请求穿过安全通道(ssl),则返回on。如果请求来自非安全通道,则返回off。

https_keysize
安全套接字层连接关键字的位数,如128。

https_secretkeysize
服务器验证私人关键字的位数。如1024。

https_server_issuer
服务器证书的发行者字段。

https_server_subject
服务器证书的主题字段。

instance_id
iis实例的id号。

instance_meta_path
响应请求的iis实例的元数据库路径。

local_addr
返回接受请求的服务器地址。

logon_user
用户登录windows nt的帐号

path_info
客户端提供的路径信息。

path_translated
通过由虚拟至物理的映射后得到的路径。

query_string
查询字符串内容。

remote_addr
发出请求的远程主机的ip地址。

remote_host
发出请求的远程主机名称。

request_method
提出请求的方法。比如get、head、post等等。

script_name
执行脚本的名称。

server_name
服务器的主机名、dns地址或ip地址。

server_port
接受请求的服务器端口号。

server_port_secure
如果接受请求的服务器端口为安全端口时,则为1,否则为0。

server_protocol
服务器使用的协议的名称和版本。

server_software
应答请求并运行网关的服务器软件的名称和版本。

url
提供url的基本部分

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网