轉:asp.net擷取用戶端登入者mac地址

來源:互聯網
上載者:User

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, sDNSNamefunction 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 "";        }    }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.