在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
本文主要是从网络上搜索的文章进行参考,也是提供一个大概的方向和思路吧。当然如果有能用的代码,可直接复制过去根据自己的情况修改和使用。
一、思路(主要用于局域网内) 通过Web获取客户端的IP地址较简单,从客户端向web服务器发送的数据报中(Request对象)中即可 而获取MAC的过程较复杂,因为客户端向服务器发送的请求对象中不包含MAC地址,因此得到两种思
(1).在客户端来获取客户机的MAC地址,所以当请求到达客户端后,通过客户端脚本来获取客户端
(2).先获取客户端IP地址,然后在局域网中发送ARP请求,通过ARP协议和已知的IP地址来解析客户
下面为获取IP地址、MAC地址的的代码, 以及通过发送ARP请求解析MAC地址的过程: https://blog.csdn.net/wang379275614/article/details/17596135 ------------------------------------------------------------------------------- 切记要引用命名空间:using System.Management。 ------------------------------------------------------------------------------- Request.UserHostAddress string IP = (Request.ServerVariables["HTTP_VIA"]!=null)?Request.ServerVariables
string ipAddress = System.Web.HttpContext.Current.Request.UserHostAddress; 先引用命名空间:using System.Net; HttpBrowserCapabilities bc= Request.Browser; https://bbs.csdn.net/topics/90213110 ------------------------------------------------------------------------------- ------------------------------------------------------------------------------ 1、获取客户端IP:Request.ServerVariables.Get("Remote_Addr").ToString(); 2、获取客户端主机名:Request.ServerVariables.Get("Remote_Host").ToString(); 3、获取客户端浏览器:Request.Browser.Browser; 4、获取客户端浏览器 版本号:Request.Browser.MajorVersion; 5、获取客户端操作系统:Request.Browser.Platform; 6、获取服务器IP:Request.ServerVariables.Get("Local_Addr").ToString(); 7、获取服务器名:Request.ServerVariables.Get("Server_Name").ToString();
foreach(String o in Request.ServerVariables){ Response.Write(o+"="+Request.ServerVariables[o]+"<br>"); }
string stringMAC = ""; string stringIP = ""; ManagementClass MC = new ManagementClass ("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection MOC= MC.GetInstances(); foreach(ManagementObject MO in MOC) { if ((bool)MO["IPEnabled"] == true) { stringMAC += MO["MACAddress"].ToString(); //获取网卡的地址 string[] IPAddresses = (string[]) MO["IPAddress"]; //获取本地的IP地址 if(IPAddresses.Length > 0) stringIP = IPAddresses[0]; Response.Write(stringMAC+"/"+stringIP); } }
//要引用到以下两个命名空间 using System.Diagnostics; using System.Text.RegularExpressions; //获取远程客户端的网卡物理地址(MAC) public string GetMac(string IP) //para IP is the client's IP { string dirResults=""; ProcessStartInfo psi = new ProcessStartInfo(); Process proc = new Process(); psi.FileName = "nbtstat"; psi.RedirectStandardInput = false; psi.RedirectStandardOutput = true; psi.Arguments = "-A " + IP; psi.UseShellExecute = false; proc = Process.Start(psi); dirResults = proc.StandardOutput.ReadToEnd(); proc.WaitForExit(); dirResults=dirResults.Replace("\r","").Replace("\n","").Replace("\t",""); Regex reg=new Regex("Mac[ ]{0,}Address[ ]{0,}=[ ]{0,}(?<key>((.)*?))__MAC",RegexOptions.IgnoreCase|RegexOptions.Compiled); Match mc=reg.Match(dirResults+"__MAC"); if(mc.Success) { return mc.Groups["key"].Value; } else { reg=new Regex("Host not found",RegexOptions.IgnoreCase|RegexOptions.Compiled); mc=reg.Match(dirResults); if(mc.Success) { return "Host not found!"; } else { return ""; } } } //在页面上打印出客户端的网卡物理地址(MAC) Response.Write(this.GetMac(Request.UserHostAddress.ToString()));
出处:https://www.cnblogs.com/xinaixia/p/4772955.html 1、获取客户端IP:Request.ServerVariables.Get("Remote_Addr").ToString(); 2、获取客户端主机名:Request.ServerVariables.Get("Remote_Host").ToString(); 3、获取客户端浏览器:Request.Browser.Browser; 4、获取客户端浏览器 版本号:Request.Browser.MajorVersion; 5、获取客户端操作系统:Request.Browser.Platform; 6、获取服务器IP:Request.ServerVariables.Get("Local_Addr").ToString(); 7、获取服务器名:Request.ServerVariables.Get("Server_Name").ToString();
foreach(String o in Request.ServerVariables){ Response.Write(o+"="+Request.ServerVariables[o]+"<br>"); }
string stringMAC = ""; string stringIP = ""; ManagementClass MC = new ManagementClass ("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection MOC= MC.GetInstances(); foreach(ManagementObject MO in MOC) { if ((bool)MO["IPEnabled"] == true) { stringMAC += MO["MACAddress"].ToString(); //获取网卡的地址 string[] IPAddresses = (string[]) MO["IPAddress"]; //获取本地的IP地址 if(IPAddresses.Length > 0) stringIP = IPAddresses[0]; Response.Write(stringMAC+"/"+stringIP); } }
//要引用到以下两个命名空间 using System.Diagnostics; using System.Text.RegularExpressions; //获取远程客户端的网卡物理地址(MAC) public string GetMac(string IP) //para IP is the client's IP { string dirResults=""; ProcessStartInfo psi = new ProcessStartInfo(); Process proc = new Process(); psi.FileName = "nbtstat"; psi.RedirectStandardInput = false; psi.RedirectStandardOutput = true; psi.Arguments = "-A " + IP; psi.UseShellExecute = false; proc = Process.Start(psi); dirResults = proc.StandardOutput.ReadToEnd(); proc.WaitForExit(); dirResults=dirResults.Replace("\r","").Replace("\n","").Replace("\t",""); Regex reg=new Regex("Mac[ ]{0,}Address[ ]{0,}=[ ]{0,}(?<key>((.)*?))__MAC",RegexOptions.IgnoreCase|RegexOptions.Compiled); Match mc=reg.Match(dirResults+"__MAC"); if(mc.Success) { return mc.Groups["key"].Value; } else { reg=new Regex("Host not found",RegexOptions.IgnoreCase|RegexOptions.Compiled); mc=reg.Match(dirResults); if(mc.Success) { return "Host not found!"; } else { return ""; } } } //在页面上打印出客户端的网卡物理地址(MAC) Response.Write(this.GetMac(Request.UserHostAddress.ToString()));
|
请发表评论