ASP.NET获取客户端网卡使用的MAC地址信息

时间:2022-10-01 16:18:12
方式一:利用.NET自带的类库直接就可以获得

[c-sharp] view plaincopy

1. using System; 

2. using System.Collections.Generic; 

3. using System.Linq; 

4. using System.Web; 

5. using System.Web.UI; 

6. using System.Web.UI.WebControls; 

7. //添加引用,并导入命名空间

8. using System.Management; 

9. namespace WebApp 

10. { 

11. public partial class WebForm1 : System.Web.UI.Page 

12.     { 

13. protected void Page_Load(object sender, EventArgs e) 

14.         { 

15.             Response.Write("计算机名称: " + System.Net.Dns.GetHostName() + "<br>"); 

16. //调用GetMacAddress函数,获取网卡地址信息

17.             Response.Write("MAC地址: " + GetMacAddress()); 

18.         } 

19. /// <summary>

20. /// 获取网卡地址信息

21. /// </summary>

22. /// <returns></returns>

23. string GetMacAddress() 

24.         { 

25. try

26.             { 

27. string mac = ""; 

28.                 ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); 

29.                 ManagementObjectCollection moc = mc.GetInstances(); 

30. foreach (ManagementObject mo in moc) 

31.                 { 

32. if ((bool)mo["IPEnabled"] == true

33.                     { 

34.                         mac = mo["MacAddress"].ToString(); 

35. break

36.                     } 

37.                 } 

38.                 moc = null

39.                 mc = null

40. return mac; 

41.             } 

42. catch

43.             { 

44. return "unknow"; 

45.             } 

46.         } 

47.     } 

48. } 

运行结果:
计算机名称: lenovo-05d886ff
MAC地址: 00:26:22:05:8C:C7

注意:上传到服务器测试,发现获得的是服务器的MAC地址

方式二:通过win32特性获得
优点:无需在客户端设置;
缺点:无法获取服务器的mac(经测试总为0,其实感觉没有必要获取服务器的mac,所以也不算缺点)(能否跨网段获取还没有测试)

[c-sharp] view plaincopy

1. using System; 

2. using System.Collections.Generic; 

3. using System.Linq; 

4. using System.Web; 

5. using System.Web.UI; 

6. using System.Web.UI.WebControls; 

7. //导入命名空间

8. using System.Runtime.InteropServices; 

9. namespace WebApp 

10. { 

11. public partial class WebForm2 : System.Web.UI.Page 

12.     { 

13.         [DllImport("Iphlpapi.dll")] 

14. private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length); 

15.         [DllImport("Ws2_32.dll")] 

16. private static extern Int32 inet_addr(string ip); 

17. private void Page_Load(object sender, System.EventArgs e) 

18.         { 

19. try

20.             { 

21. string userip = Request.UserHostAddress; 

22. string strClientIP = Request.UserHostAddress.ToString().Trim(); 

23.                 Int32 ldest = inet_addr(strClientIP); //目的地的ip

24.                 Int64 macinfo = new Int64(); 

25.                 Int32 len = 6; 

26. int res = SendARP(ldest, 0, ref macinfo, ref len); 

27. string mac_src = macinfo.ToString("X"); 

28. if (mac_src == "0") 

29.                 { 

30. if (userip == "127.0.0.1") 

31.                     { 

32.                         Response.Write("正在访问Localhost!"); 

33.                     } 

34. else

35.                     { 

36.                         Response.Write("欢迎来自IP为" + userip + "的朋友!" + "<br>"); 

37.                     } 

38. return

39.                 } 

40. while (mac_src.Length < 12) 

41.                 { 

42.                     mac_src = mac_src.Insert(0, "0"); 

43.                 } 

44. string mac_dest = ""; 

45. for (int i = 0; i < 11; i++) 

46.                 { 

47. if (0 == (i % 2)) 

48.                     { 

49. if (i == 10) 

50.                         { 

51.                             mac_dest = mac_dest.Insert(0, mac_src.Substring(i, 2)); 

52.                         } 

53. else

54.                         { 

55.                             mac_dest = "-" + mac_dest.Insert(0, mac_src.Substring(i, 2)); 

56.                         } 

57.                     } 

58.                 } 

59.                 Response.Write("欢迎来自IP为" + userip + "<br>" + ",MAC地址为" + mac_dest + "的朋友!" + "<br>"); 

60.             } 

61. catch (Exception err) 

62.             { 

63.                 Response.Write(err.Message); 

64.             } 

65.         } 

66.     } 

67. } 

运行结果:
正在访问Localhost!

注意:上传到服务器测试,发现获得的是服务器的MAC地址

方式三:通过javascript加载ActiveX来实现:

优点:任意获取mac地址(可以跨网段获取);

缺点:需要在客户端浏览器中将activeX的选项全部打开,否则获取不到,所以不怎么稳定;

前台代码:(WebForm3.aspx)

[xhtml] view plaincopy

1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApp.WebForm3" %>

2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

3. <html xmlns="http://www.w3.org/1999/xhtml" >

4. <head runat="server">

5. <title></title>

6. <mce:script language="javascript"><!-- 

7.         document.write("<OBJECT id='locator' classid='CLSID:76A64158-CB41-11D1-8B02-00600806D9B6' VIEWASTEXT></OBJECT>"); 

8.         document.write("<OBJECT id=foo classid=CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223></OBJECT>"); 

9.         var MACAddr, IPAddr, sDNSName 

10.         function getObject(objObject, objAsyncContext) { 

11.             try { 

12.                 if (MACAddr == null && objObject.IPEnabled) { 

13.                     if (objObject.MACAddress != null && objObject.MACAddress != "undefined") 

14. MACAddr = objObject.MACAddress; 

15.                     if (objObject.IPEnabled && objObject.IPAddress(0) != null && objObject.IPAddress(0) != "undefined") 

16. IPAddr = objObject.IPAddress(0); 

17.                     if (objObject.DNSHostName != null && objObject.DNSHostName != "undefined") 

18. sDNSName = objObject.DNSHostName; 

19.                 } 

20.             } 

21.             catch (err) 

22.             { } 

23.         } 

24.         function setValue(hResult, pErrorObject, pAsyncContext) { 

25.             createTxt("txtMAC", MACAddr); 

26.             createTxt("txtIp", IPAddr); 

27.             createTxt("txtPCName", sDNSName); 

28.         } 

29.         function createTxt(txtName, txtValue) { 

30.             var macTxt = document.createElement("INPUT"); 

31. macTxt.name = txtName; 

32. macTxt.value = txtValue; 

33. macTxt.type = "hidden"; 

34.             try { 

35.                 document.forms[0].appendChild(macTxt); 

36.             } 

37.             catch (err) { 

38.                 //获取失败

39.             } 

40.         } 

41.         document.getElementById("foo").attachEvent("OnObjectReady", getObject); 

42.         document.getElementById("foo").attachEvent("OnCompleted", setValue); 

43.         try { 

44.             var service = locator.ConnectServer(); 

45.             var MACAddr; 

46.             var IPAddr; 

47.             var DomainAddr; 

48.             var sDNSName; 

49. service.Security_.ImpersonationLevel = 3; 

50.             service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration'); 

51.         } 

52.         catch (err) { 

53.             //获取失败

54.         } 

55.

56. // --></mce:script>

57. </head>

58. <body>

59. <form id="form1" runat="server">

60. <div>

61.

62. <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

63.

64. </div>

65. </form>

66. </body>

67. </html>

后台代码:(WebForm3.aspx.cs)

[c-sharp] view plaincopy

1. using System; 

2. using System.Collections.Generic; 

3. using System.Linq; 

4. using System.Web; 

5. using System.Web.UI; 

6. using System.Web.UI.WebControls; 

7. namespace WebApp 

8. { 

9. public partial class WebForm3 : System.Web.UI.Page 

10.     { 

11. protected void Page_Load(object sender, EventArgs e) 

12.         { 

13.         } 

14. /// <summary>

15. /// 获取mac地址

16. /// </summary>

17. /// <returns></returns>

18. public string GetMACs() 

19.         { 

20. try

21.             { 

22. return Request.Form["txtMAC"].ToString(); 

23.             } 

24. catch

25.             { 

26. return ""; 

27.             } 

28.         } 

29. protected void Button1_Click(object sender, EventArgs e) 

30.         { 

31.             Response.Write(GetMACs()); 

32.         } 

33.     } 

34. } 

运行提示:

ASP.NET获取客户端网卡使用的MAC地址信息

启用未标记为可安全执行脚本的ActiveX
第一步:工具->Internet选项
第二步:安全->Internet->自定义级别
第三步:启用“对未标记为可安全执行脚本的ActiveX控件初始化并执行”,如下图所示:

ASP.NET获取客户端网卡使用的MAC地址信息

运行结果:

ASP.NET获取客户端网卡使用的MAC地址信息

总结:感觉第一种方法还是方便,第二种方法获得不到服务器的MAC地址,第三种方法还需设置,两个字“不爽”。

参考:http://www.xueit.com/asp.net/show-6092-1.aspx