Asp.net Gets the mac address of the client login

Links to the original text: http://www.cnblogs.com/LeeYongze/archive/2010/11/18/1880990.html

There are two ways for asp.net to get the mac address of the client login

1. Through the win32 feature,

Advantages: No need to set up in the client; Disadvantage: unable to access the server's MAC (after testing total 0, actually feel no need to access the server's mac, so it is not a disadvantage) (whether cross-section access has not been tested)

2. ActiveX is loaded through javascript to achieve:

Advantages: arbitrary access to mac address (can be obtained across network segments); disadvantages: need to open all the options of activeX in the client browser, otherwise it can not be obtained, so it is not stable;

Through the win32 feature:

 

  1.  [DllImport("Iphlpapi.dll")]     
  2.   
  3. 02       private static extern int SendARP(Int32 dest,Int32 host,ref Int64 mac,ref Int32 length);     
  4.   
  5. 03       [DllImport("Ws2_32.dll")]    
  6.   
  7. 04       private static extern Int32 inet_addr(string ip);    
  8.   
  9. 05      
  10.   
  11. 06       private void Page_Load(object sender, System.EventArgs e)    
  12.   
  13. 07       {    
  14.   
  15. 08            try   
  16.   
  17. 09            {    
  18.   
  19. 10                 string userip=Request.UserHostAddress;    
  20.   
  21. 11                 string strClientIP = Request.UserHostAddress.ToString().Trim();    
  22.   
  23. 12                 Int32 ldest = inet_addr(strClientIP); //Destination ip  
  24.   
  25. 13                 Int64 macinfo = new Int64();     
  26.   
  27. 14                 Int32 len = 6;     
  28.   
  29. 15                 int res = SendARP(ldest,0, ref macinfo, ref len);    
  30.   
  31. 16                 string mac_src=macinfo.ToString("X");    
  32.   
  33. 17                 if(mac_src == "0")    
  34.   
  35. 18                 {    
  36.   
  37. 19                     if (userip == "127.0.0.1")    
  38.   
  39. 20                     {    
  40.   
  41. 21                         Response.Write("Visiting Localhost!");    
  42.   
  43. 22                     }    
  44.   
  45. 23                     else   
  46.   
  47. 24                     {    
  48.   
  49. 25                         Response.Write("Welcome from IP by" + userip + "Friend!" + "<br>");    
  50.   
  51. 26                     }    
  52.   
  53. 27                      return;    
  54.   
  55. 28                 }    
  56.   
  57. 29      
  58.   
  59. 30                 while(mac_src.Length<12)    
  60.   
  61. 31                 {    
  62.   
  63. 32                     mac_src = mac_src.Insert(0,"0");    
  64.   
  65. 33                 }    
  66.   
  67. 34      
  68.   
  69. 35                 string mac_dest="";    
  70.   
  71. 36      
  72.   
  73. 37                 for(int i=0;i<11;i++)    
  74.   
  75. 38                 {    
  76.   
  77. 39                      if (0 == (i % 2))    
  78.   
  79. 40                      {    
  80.   
  81. 41                           if ( i == 10 )    
  82.   
  83. 42                           {    
  84.   
  85. 43                                 mac_dest = mac_dest.Insert(0,mac_src.Substring(i,2));    
  86.   
  87. 44                           }    
  88.   
  89. 45                           else   
  90.   
  91. 46                           {    
  92.   
  93. 47                                 mac_dest ="-" + mac_dest.Insert(0,mac_src.Substring(i,2));    
  94.   
  95. 48                           }    
  96.   
  97. 49                      }     
  98.   
  99. 50                 }    
  100.   
  101. 51      
  102.   
  103. 52                 Response.Write ("Welcome from IP by"+userip+ "<br>" + ",MAC Address is"+mac_dest+"Friend!"    
  104.   
  105. 53      
  106.   
  107. 54                  +   "<br>");    
  108.   
  109. 55            }    
  110.   
  111. 56            catch(Exception err)     
  112.   
  113. 57            {     
  114.   
  115. 58                 Response.Write(err.Message);    
  116.   
  117. 59            }    
  118.   
  119. 60       }   

 

Client mac can be obtained through the above code, which needs to be on the client to see the effect.

ActiveX is loaded through javascript to achieve:

The javascript code is as follows

  1. document.write("<OBJECT id='locator' classid='CLSID:76A64158-CB41-11D1-8B02-00600806D9B6' VIEWASTEXT></OBJECT>");    
  2.   
  3. 04 document.write("<OBJECT id=foo classid=CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223></OBJECT>");    
  4.   
  5. 05 var MACAddr, IPAddr, sDNSName    
  6.   
  7. 06 function getObject(objObject,objAsyncContext){    
  8.   
  9. 07     try{    
  10.   
  11. 08         if(MACAddr==null && objObject.IPEnabled)    
  12.   
  13. 09         {    
  14.   
  15. 10            if(objObject.MACAddress != null && objObject.MACAddress != "undefined")    
  16.   
  17. 11                MACAddr = objObject.MACAddress;    
  18.   
  19. 12            if(objObject.IPEnabled && objObject.IPAddress(0) != null && objObject.IPAddress(0) != "undefined")    
  20.   
  21. 13                IPAddr = objObject.IPAddress(0);    
  22.   
  23. 14            if(objObject.DNSHostName != null && objObject.DNSHostName != "undefined")    
  24.   
  25. 15                sDNSName = objObject.DNSHostName;    
  26.   
  27. 16         }    
  28.   
  29. 17     }    
  30.   
  31. 18     catch(err)    
  32.   
  33. 19     {   }    
  34.   
  35. 20 }    
  36.   
  37. 21 function setValue(hResult,pErrorObject, pAsyncContext){    
  38.   
  39. 22     createTxt("txtMAC", MACAddr);    
  40.   
  41. 23     createTxt("txtIp", IPAddr);    
  42.   
  43. 24     createTxt("txtPCName", sDNSName);    
  44.   
  45. 25 }    
  46.   
  47. 26 function createTxt(txtName, txtValue){    
  48.   
  49. 27     var macTxt = document.createElement("INPUT");    
  50.   
  51. 28     macTxt.name = txtName;    
  52.   
  53. 29     macTxt.value = txtValue;    
  54.   
  55. 30     macTxt.type = "hidden";    
  56.   
  57. 31     try   
  58.   
  59. 32     {    
  60.   
  61. 33         document.forms[0].appendChild(macTxt);    
  62.   
  63. 34     }    
  64.   
  65. 35     catch(err)    
  66.   
  67. 36     {    
  68.   
  69. 37          
  70.   
  71. 38         //Acquisition failure  
  72.   
  73. 39     }    
  74.   
  75. 40 }    
  76.   
  77. 41      
  78.   
  79. 42 document.getElementById("foo").attachEvent("OnObjectReady", getObject);    
  80.   
  81. 43 document.getElementById("foo").attachEvent("OnCompleted", setValue);    
  82.   
  83. 44      
  84.   
  85. 45 try   
  86.   
  87. 46 {    
  88.   
  89. 47     var service = locator.ConnectServer();    
  90.   
  91. 48     var MACAddr ;    
  92.   
  93. 49     var IPAddr ;    
  94.   
  95. 50     var DomainAddr;    
  96.   
  97. 51     var sDNSName;    
  98.   
  99. 52     service.Security_.ImpersonationLevel=3;    
  100.   
  101. 53     service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration');     
  102.   
  103. 54 }    
  104.   
  105. 55 catch(err)    
  106.   
  107. 56 {    
  108.   
  109. 57     //Acquisition failure  
  110.   
  111. 58 }   

 

Referring to the above js on the aspx page you want to retrieve, you can retrieve the max address on that page by using the following methods

  1.   /// <summary>    
  2.   
  3. 02     /// Get mac address  
  4.   
  5. 03     /// </summary>    
  6.   
  7. 04     /// <returns></returns>    
  8.   
  9. 05     public string GetMACs()    
  10.   
  11. 06     {    
  12.   
  13. 07          try   
  14.   
  15. 08         {    
  16.   
  17. 09             return Request.Form["txtMAC"].ToString();    
  18.   
  19. 10         }    
  20.   
  21. 11         catch   
  22.   
  23. 12         {    
  24.   
  25. 13      
  26.   
  27. 14             return "";    
  28.   
  29. 15         }    
  30.   
  31. 16     }   

 

Turn from http://www.cnblogs.com/davinci/archive/2010/01/20/1652171.html

Reprinted at: https://www.cnblogs.com/Lee Yongze/archive/2010/11/18/1880990.html

Tags: Mac Javascript network

Posted on Sun, 06 Oct 2019 09:31:55 -0700 by DigitalNinja