项目中要求得到客户端的MAC地址及IP地址,但找了网上很多的例子,但经测试过之后得到的都是服务器的代码,后来看到一篇文章中说到,因为这网页上运行的服务器端的代码,所以无法得到客户端的的MAC地址。
下面是查到的一些资料,但是都是得到客户端的代码,下面是一些网上找到这些代码的集锦:
Label1.Text = "//客户端ip:" + Request.ServerVariables.Get("Remote_Addr").ToString() + "<br>" + " //客户端主机名:" + Request.ServerVariables.Get("Remote_Host").ToString() + "<br>" + "//客户端浏览器IE:" + Request.Browser.Browser + "<br>" + "//客户端浏览器 版本号:" + Request.Browser.MajorVersion + "<br>" + "//客户端操作系统:" + Request.Browser.Platform + "<br>" + "//服务器ip:" + Request.ServerVariables.Get("Local_Addr").ToString() + "<br>" + "//服务器名:" + Request.ServerVariables.Get("Server_Name").ToString() + "<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]; Label1.Text += "<br>MAC:" + stringMAC + "/ IP:" + stringIP; if (stringIP == "192.168.1.45") Label1.Text += "<br>I get you :192.168.1.45<br>"; } }
所以感觉从服务器是无法得到其IP地址,下面是用Javascript的方法到其Mac地址,可惜这个是需要用交互Active Object的!下面是用Javascript实现的代码
Javascript Get Mac
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>JS获取客户端MAC地址</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <meta name="generator" content="editplus" /> <meta name="author" content="" /> <meta name="keywords" content="" /> <meta name="description" content="" /> <style type="text/css"> </style> <script event="OnObjectReady(objObject,objAsyncContext)" for="foo"> if(objObject.IPEnabled != null && objObject.IPEnabled != "undefined" && objObject.IPEnabled == true) { if(objObject.MACAddress != null && objObject.MACAddress != "undefined" && objObject.DNSServerSearchOrder!=null) MACAddr = objObject.MACAddress; if(objObject.IPEnabled && objObject.IPAddress(0) != null && objObject.IPAddress(0) != "undefined"&& objObject.DNSServerSearchOrder!=null) IPAddr = objObject.IPAddress(0); if(objObject.DNSHostName != null && objObject.DNSHostName != "undefined") sDNSName = objObject.DNSHostName; } </script> <script type="text/javascript"> var MACAddr ; var IPAddr ; var DomainAddr; var sDNSName; function init() { var service = locator.ConnectServer(); service.Security_.ImpersonationLevel=3; service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration'); } function getMac() { document.getElementById('txtMac').value = unescape(MACAddr); } getMac(); </script> </head> <body onload="init()"> <object id="locator" classid="CLSID:76A64158-CB41-11D1-8B02-00600806D9B6" VIEWASTEXT></object> <object id="foo" classid="CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223"></object> <input type="text" id="txtMac" /> <input type="button" id="btn" value="获取Mac地址" onclick="getMac();" /> </body> </html>
Note: 用 Request.UserHostAddress可以得到IP地址,但如果你的客户端系统是window2008 r2的话,那么得到IP地址会是IPv6的.
参考了以下网址:
http://topic.csdn.net/u/20100807/09/58faa224-4f26-4177-9ddc-5df8a0292f14.html