Asp.net获取客户端登录者mac地址
asp.net获取客户端登录者的mac地址,有两种方法
1,通过win32特性获得,
优点:无需在客户端设置;缺点:无法获取服务器的mac(经测试总为0,其实感觉没有必要获取服务器的mac,所以也不算缺点)(能否跨网段获取还没有测试)
2,通过javascript加载ActiveX来实现:
优点:任意获取mac地址(可以跨网段获取);缺点:需要在客户端浏览器中将activeX的选项全部打开,否则获取不到,所以不怎么稳定;
通过win32特性获得:
- [DllImport("Iphlpapi.dll")]
- 02 private static extern int SendARP(Int32 dest,Int32 host,ref Int64 mac,ref Int32 length);
- 03 [DllImport("Ws2_32.dll")]
- 04 private static extern Int32 inet_addr(string ip);
- 05
- 06 private void Page_Load(object sender, System.EventArgs e)
- 07 {
- 08 try
- 09 {
- 10 string userip=Request.UserHostAddress;
- 11 string strClientIP = Request.UserHostAddress.ToString().Trim();
- 12 Int32 ldest = inet_addr(strClientIP); //目的地的ip
- 13 Int64 macinfo = new Int64();
- 14 Int32 len = 6;
- 15 int res = SendARP(ldest,0, ref macinfo, ref len);
- 16 string mac_src=macinfo.ToString("X");
- 17 if(mac_src == "0")
- 18 {
- 19 if (userip == "127.0.0.1")
- 20 {
- 21 Response.Write("正在访问Localhost!");
- 22 }
- 23 else
- 24 {
- 25 Response.Write("欢迎来自IP为" + userip + "的朋友!" + "<br>");
- 26 }
- 27 return;
- 28 }
- 29
- 30 while(mac_src.Length<12)
- 31 {
- 32 mac_src = mac_src.Insert(0,"0");
- 33 }
- 34
- 35 string mac_dest="";
- 36
- 37 for(int i=0;i<11;i++)
- 38 {
- 39 if (0 == (i % 2))
- 40 {
- 41 if ( i == 10 )
- 42 {
- 43 mac_dest = mac_dest.Insert(0,mac_src.Substring(i,2));
- 44 }
- 45 else
- 46 {
- 47 mac_dest ="-" + mac_dest.Insert(0,mac_src.Substring(i,2));
- 48 }
- 49 }
- 50 }
- 51
- 52 Response.Write ("欢迎来自IP为"+userip+ "<br>" + ",MAC地址为"+mac_dest+"的朋友!"
- 53
- 54 + "<br>");
- 55 }
- 56 catch(Exception err)
- 57 {
- 58 Response.Write(err.Message);
- 59 }
- 60 }
通过以上代码则可获取客户端mac,需要在客户机子上面才能看到效果
通过javascript加载ActiveX来实现:
javascript代码如下
- document.write("<OBJECT id='locator' classid='CLSID:76A64158-CB41-11D1-8B02-00600806D9B6' VIEWASTEXT></OBJECT>");
- 04 document.write("<OBJECT id=foo classid=CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223></OBJECT>");
- 05 var MACAddr, IPAddr, sDNSName
- 06 function getObject(objObject,objAsyncContext){
- 07 try{
- 08 if(MACAddr==null && objObject.IPEnabled)
- 09 {
- 10 if(objObject.MACAddress != null && objObject.MACAddress != "undefined")
- 11 MACAddr = objObject.MACAddress;
- 12 if(objObject.IPEnabled && objObject.IPAddress(0) != null && objObject.IPAddress(0) != "undefined")
- 13 IPAddr = objObject.IPAddress(0);
- 14 if(objObject.DNSHostName != null && objObject.DNSHostName != "undefined")
- 15 sDNSName = objObject.DNSHostName;
- 16 }
- 17 }
- 18 catch(err)
- 19 { }
- 20 }
- 21 function setValue(hResult,pErrorObject, pAsyncContext){
- 22 createTxt("txtMAC", MACAddr);
- 23 createTxt("txtIp", IPAddr);
- 24 createTxt("txtPCName", sDNSName);
- 25 }
- 26 function createTxt(txtName, txtValue){
- 27 var macTxt = document.createElement("INPUT");
- 28 macTxt.name = txtName;
- 29 macTxt.value = txtValue;
- 30 macTxt.type = "hidden";
- 31 try
- 32 {
- 33 document.forms[0].appendChild(macTxt);
- 34 }
- 35 catch(err)
- 36 {
- 37
- 38 //获取失败
- 39 }
- 40 }
- 41
- 42 document.getElementById("foo").attachEvent("OnObjectReady", getObject);
- 43 document.getElementById("foo").attachEvent("OnCompleted", setValue);
- 44
- 45 try
- 46 {
- 47 var service = locator.ConnectServer();
- 48 var MACAddr ;
- 49 var IPAddr ;
- 50 var DomainAddr;
- 51 var sDNSName;
- 52 service.Security_.ImpersonationLevel=3;
- 53 service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration');
- 54 }
- 55 catch(err)
- 56 {
- 57 //获取失败
- 58 }
在要获取的aspx页面上面引用以上js,可以在该页面通过以下方法来获取max地址
- /// <summary>
- 02 /// 获取mac地址
- 03 /// </summary>
- 04 /// <returns></returns>
- 05 public string GetMACs()
- 06 {
- 07 try
- 08 {
- 09 return Request.Form["txtMAC"].ToString();
- 10 }
- 11 catch
- 12 {
- 13
- 14 return "";
- 15 }
- 16 }
转自http://www.cnblogs.com/davinci/archive/2010/01/20/1652171.html
作者:Mr S.R Lee
出处:http://www.cnblogs.com/LeeYongze
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.