.NET获取客户端的操作系统、IP地址、浏览器版本

获取客户端的操作系统:

#region 获取操作系统版本号

/// <summary> 
/// 获取操作系统版本号 
/// </summary> 
/// <returns></returns>

public static string GetOSVersion()
{
  //UserAgent 
  var userAgent = HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"];

  var osVersion = "未知";
  if (userAgent.Contains("NT 10.0"))
  {
    osVersion = "Windows 10";
  }
  else if (userAgent.Contains("NT 6.3"))
  {
    osVersion = "Windows 8.1";
  }
  else if (userAgent.Contains("NT 6.2"))
  {
    osVersion = "Windows 8";
  }

  else if (userAgent.Contains("NT 6.1"))
  {
    osVersion = "Windows 7";
  }
  else if (userAgent.Contains("NT 6.0"))
  {
    osVersion = "Windows Vista/Server 2008";
  }
  else if (userAgent.Contains("NT 5.2"))
  {
    osVersion = "Windows Server 2003";
  }
  else if (userAgent.Contains("NT 5.1"))
  {
    osVersion = "Windows XP";
  }
  else if (userAgent.Contains("NT 5"))
  {
    osVersion = "Windows 2000";
  }
  else if (userAgent.Contains("NT 4"))
  {
    osVersion = "Windows NT4";
  }
  else if (userAgent.Contains("Me"))
  {
    osVersion = "Windows Me";
  }
  else if (userAgent.Contains("98"))
  {
    osVersion = "Windows 98";
  }
  else if (userAgent.Contains("95"))
  {
    osVersion = "Windows 95";
  }
  else if (userAgent.Contains("Mac"))
  {
    osVersion = "Mac";
  }
  else if (userAgent.Contains("Unix"))
  {
    osVersion = "UNIX";
  }
  else if (userAgent.Contains("Linux"))
  {
    osVersion = "Linux";
  }
  else if (userAgent.Contains("SunOS"))
  {
    osVersion = "SunOS";
  }
  return osVersion;
}
#endregion

获取客户端的IP地址:

#region 获取IP地址

/// <summary> 
/// 获取IP地址
/// </summary> 
/// <returns></returns>

public static string GetIPAddress()
{
  string ipv4 = String.Empty;
  foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
  {
    if (IPA.AddressFamily.ToString() == "InterNetwork")
    {
      ipv4 = IPA.ToString();
      break;
    }
  }
  if (ipv4 != String.Empty)
  {
    return ipv4;
  }
  foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
  {
    if (IPA.AddressFamily.ToString() == "InterNetwork")
    {
      ipv4 = IPA.ToString();
      break;
    }
  }
  return ipv4;
}

#endregion

获取客户端的浏览器版本:

#region 获取浏览器版本号

/// <summary> 
/// 获取浏览器版本号 
/// </summary> 
/// <returns></returns> 
public static string GetBrowser()
{ 
  HttpBrowserCapabilities bc = HttpContext.Current.Request.Browser;
  return bc.Browser + bc.Version;
}

#endregion

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
获取客户的物理地址是一个比较常见的需求,可以通过以下两种方式来实现: 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对象。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值