ASP.NET 中获取客户端IP地址

9 篇文章 1 订阅
ip获取,通过以下几种方式
   1:  HttpContext.Current.Request.ServerVariables["HTTP_VIA"];
   2:  HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];       
   3:  HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; 
   4:  HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"];
   5:  HttpContext.Current.Request.UserHostAddress;

但 不同情况下,是要分别对待的,如

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

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

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

      REMOTE_ADDR = 最后一个代理服务器 IP
      HTTP_VIA = 代理服务器 IP
      HTTP_X_FORWARDED_FOR = 用户的真实 IP ,多个ip地址中取第一个

     这类代理服务器还是将您的信息转发给您的访问对象,无法达到隐藏真实身份的目的。

三、使用普通匿名代理服务器的情况:Anonymous Proxies

      REMOTE_ADDR = 最后一个代理服务器 IP
      HTTP_VIA = 代理服务器 IP
      HTTP_X_FORWARDED_FOR = 代理服务器 IP ,多个ip地址中取第一个

     隐藏了您的真实IP,但是向访问对象透露了您是使用代理服务器访问他们的。

四、使用欺骗性代理服务器的情况:Distorting Proxies

      REMOTE_ADDR = 代理服务器 IP
      HTTP_VIA = 代理服务器 IP
      HTTP_X_FORWARDED_FOR = 随机的 IP ,  
 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取客户端的物理地址是一个比较常见的需求,可以通过以下两种方式来实现: 1. 使用ARP协议获取物理地址 在局域网,可以通过ARP协议获取客户端的物理地址。具体实现方式可以参考以下代码: ```csharp using System.Net; using System.Net.NetworkInformation; public static string GetClientMacAddress(string clientIpAddress) { var macAddress = string.Empty; var ipAddress = IPAddress.Parse(clientIpAddress); var networkInterface = NetworkInterface.GetAllNetworkInterfaces() .FirstOrDefault(n => n.OperationalStatus == OperationalStatus.Up && n.GetIPProperties().GatewayAddresses.Any(g => g.Address.AddressFamily == ipAddress.AddressFamily)); if (networkInterface != null) { var arp = new ARP(); macAddress = arp.Resolve(ipAddress, networkInterface.GetIPProperties().GatewayAddresses.First().Address); } return macAddress; } public class ARP { [DllImport("iphlpapi.dll", ExactSpelling = true)] public static extern int SendARP(int destIp, int srcIp, byte[] macAddress, ref uint physicalAddrLen); public string Resolve(IPAddress ipAddress, IPAddress gatewayAddress) { var macAddress = new byte[6]; uint macAddrLen = (uint)macAddress.Length; var destIp = BitConverter.ToInt32(ipAddress.GetAddressBytes(), 0); var srcIp = BitConverter.ToInt32(gatewayAddress.GetAddressBytes(), 0); if (SendARP(destIp, srcIp, macAddress, ref macAddrLen) == 0) { var mac = BitConverter.ToString(macAddress, 0, (int)macAddrLen); return mac.Replace("-", ":"); } else { return string.Empty; } } } ``` 2. 使用JavaScript获取物理地址 在Web应用程序,可以通过JavaScript获取客户端的物理地址。具体实现方式可以参考以下代码: ```javascript function getClientMacAddress() { var macAddress = ""; var obj = new ActiveXObject("WbemScripting.SWbemLocator"); var service = obj.ConnectServer("."); var properties = service.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'True'"); var e = new Enumerator(properties); while (!e.atEnd()) { var p = e.item(); macAddress = p.MACAddress; break; } return macAddress; } ``` 需要注意的是,使用JavaScript获取物理地址需要在IE浏览器运行,其他浏览器不支持ActiveXObject对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值