在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
asp.net获取客户端登录者的mac地址,有两种方法 1,通过win32特性获得, 优点:无需在客户端设置;缺点:无法获取服务器的mac(经测试总为0,其实感觉没有必要获取服务器的mac,所以也不算缺点)(能否跨网段获取还没有测试) 2,通过javascript加载ActiveX来实现: 优点:任意获取mac地址(可以跨网段获取);缺点:需要在客户端浏览器中将activeX的选项全部打开,否则获取不到,所以不怎么稳定; 通过win32特性获得: [DllImport("Iphlpapi.dll")] private static extern int SendARP(Int32 dest,Int32 host,ref Int64 mac,ref Int32 length); [DllImport("Ws2_32.dll")] private static extern Int32 inet_addr(string ip); private void Page_Load(object sender, System.EventArgs e) { try { string userip=Request.UserHostAddress; string strClientIP = Request.UserHostAddress.ToString().Trim(); Int32 ldest = inet_addr(strClientIP); //目的地的ip Int64 macinfo = new Int64(); Int32 len = 6; int res = SendARP(ldest,0, ref macinfo, ref len); string mac_src=macinfo.ToString("X"); if(mac_src == "0") { if (userip == "127.0.0.1") { Response.Write("正在访问Localhost!"); } else { Response.Write("欢迎来自IP为" + userip + "的朋友!" + "<br>"); } return; } while(mac_src.Length<12) { mac_src = mac_src.Insert(0,"0"); } string mac_dest=""; for(int i=0;i<11;i++) { if (0 == (i % 2)) { if ( i == 10 ) { mac_dest = mac_dest.Insert(0,mac_src.Substring(i,2)); } else { mac_dest ="-" + mac_dest.Insert(0,mac_src.Substring(i,2)); } } } Response.Write ("欢迎来自IP为"+userip+ "<br>" + ",MAC地址为"+mac_dest+"的朋友!" + "<br>"); } catch(Exception err) { Response.Write(err.Message); } } 通过以上代码则可获取客户端mac,需要在客户机子上面才能看到效果 通过javascript加载ActiveX来实现: javascript代码如下 // JScript 文件 document.write("<OBJECT id='locator' classid='CLSID:76A64158-CB41-11D1-8B02-00600806D9B6' VIEWASTEXT></OBJECT>"); document.write("<OBJECT id=foo classid=CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223></OBJECT>"); var MACAddr, IPAddr, sDNSName function getObject(objObject,objAsyncContext){ try{ if(MACAddr==null && objObject.IPEnabled) { if(objObject.MACAddress != null && objObject.MACAddress != "undefined") MACAddr = objObject.MACAddress; if(objObject.IPEnabled && objObject.IPAddress(0) != null && objObject.IPAddress(0) != "undefined") IPAddr = objObject.IPAddress(0); if(objObject.DNSHostName != null && objObject.DNSHostName != "undefined") sDNSName = objObject.DNSHostName; } } catch(err) { } } function setValue(hResult,pErrorObject, pAsyncContext){ createTxt("txtMAC", MACAddr); createTxt("txtIp", IPAddr); createTxt("txtPCName", sDNSName); } function createTxt(txtName, txtValue){ var macTxt = document.createElement("INPUT"); macTxt.name = txtName; macTxt.value = txtValue; macTxt.type = "hidden"; try { document.forms[0].appendChild(macTxt); } catch(err) { //获取失败 } } document.getElementById("foo").attachEvent("OnObjectReady", getObject); document.getElementById("foo").attachEvent("OnCompleted", setValue); try { var service = locator.ConnectServer(); var MACAddr ; var IPAddr ; var DomainAddr; var sDNSName; service.Security_.ImpersonationLevel=3; service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration'); } catch(err) { //获取失败 } 在要获取的aspx页面上面引用以上js,可以在该页面通过以下方法来获取max地址 /// <summary> /// 获取mac地址 /// </summary> /// <returns></returns> public string GetMACs() { try { return Request.Form["txtMAC"].ToString(); } catch { return ""; } } |
请发表评论