惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
Help Net Security
Help Net Security
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
C
Cisco Blogs
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
L
LINUX DO - 热门话题
Security Latest
Security Latest
A
Arctic Wolf
G
GRAHAM CLULEY
月光博客
月光博客
S
Securelist
D
Docker
J
Java Code Geeks
T
Troy Hunt's Blog
T
Tenable Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
SecWiki News
SecWiki News
S
Security @ Cisco Blogs
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LINUX DO - 最新话题
Recent Commits to openclaw:main
Recent Commits to openclaw:main
aimingoo的专栏
aimingoo的专栏
博客园 - 【当耐特】
H
Heimdal Security Blog
The Hacker News
The Hacker News
博客园 - 三生石上(FineUI控件)
Application and Cybersecurity Blog
Application and Cybersecurity Blog
N
Netflix TechBlog - Medium
Vercel News
Vercel News
Forbes - Security
Forbes - Security
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
B
Blog
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
S
Secure Thoughts
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Check Point Blog
云风的 BLOG
云风的 BLOG
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Blog of Author Tim Ferriss
L
Lohrmann on Cybersecurity
F
Full Disclosure
D
Darknet – Hacking Tools, Hacker News & Cyber Security
P
Proofpoint News Feed

博客园 - 末世纪狂潮

[导入]Pylogs v1.14beta (正式支持Django 1.0) 发布 [导入]Django中动态改变ImageField和FileField中文件的上传路径 [导入]Tech·ED 2008 票到了。。。 [导入]Windows Live Mail不能启动的解决办法 - 末世纪狂潮 [导入]Linux Mint中文语言包 [导入]XPS 1530安装MAC OsX 10.5.2指南 [导入]奥运加油,中国加油 [导入]IE7打不开微软的首页了? [导入]linux下常用格式的压缩与解压方法 [导入]利用PIL生成水印图片或文字 [导入]Pylogs v1.12beta发布 test live write write blog 用C#写一个简单的WINDOWS服务程序 测试tb .NET中实现无刷新客户端联动下拉菜单 "百万首页"深度思考 QQ天气预报代码 QQ登录没反应解决方法,及新版QQ的一个BUG 要开始学JAVA了,郁闷中。。。
[导入]ASP.NET获取客户端IP地址的几点补充[转载] - 末世纪狂潮 - 博客园
末世纪狂潮 · 2008-09-25 · via 博客园 - 末世纪狂潮

通常我们都通过下面的代码获得IP:
   string ip =System.Web.HttpContext.Current.Request.UserHostAddress;
    或 string ip =System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

REMOTE_ADDR 说明:

访问客户端的 IP 地址。
此项信息用户不可以修改。
如果真的给改了的话,你也和服务器连接不了了,服务器就是按照这个来与客户端建立连接并进行通讯的。实际我测试修改这个 ServerVariables , 一点效果都没有。仍然获得是实际的值。

但如果用户使用了代理服务器,上述代码获得的是代理服务器的IP地址;如果用户使用了多个代理服务器,则是到达服务器的最后一个代理服务器的IP地址。

如何绕过代理服务器获得用户真实的IP地址呢?

    
private
static string getIp()
 
     if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)   
      return System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(new char[] { ',' })[0];      
    else
  
      return System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; 
 }

Request.ServerVariables["HTTP_VIA"] ,ServerVariables["HTTP_X_FORWARDED_FOR"],Request.ServerVariables["REMOTE_ADDR"]的值分下面几种情况:

一、没有使用代理服务器的情况:

      REMOTE_ADDR = 用户的 IP
      HTTP_VIA = 没数值或不显示
      HTTP_X_FORWARDED_FOR = 没数值或不显示

二、使用透明代理服务器的情况:Transparent Proxies

      REMOTE_ADDR = 最后一个代理服务器 IP
      HTTP_VIA = 代理服务器 IP
      HTTP_X_FORWARDED_FOR ...

文章来源:http://oteam.cn/2008/9/25/aspdotnet-get-client-ip-address/