方式一:利用.NET自带的类库直接就可以获得
[c-sharp]view plaincopy
1.usingSystem;
2.usingSystem.Collections.Generic;
3.usingSystem.Linq;
4.usingSystem.Web;
5.usingSystem.Web.UI;
6.usingSystem.Web.UI.WebControls;
7. //添加引用,并导入命名空间
8.usingSystem.Management;
9.namespaceWebApp
10. {
11.publicpartialclassWebForm1 : System.Web.UI.Page
12. {
13.protectedvoidPage_Load(objectsender, EventArgs e)
14. {
15. Response.Write("计算机名称: " + .Dns.GetHostName() + "<br>");
16. //调用GetMacAddress函数,获取网卡地址信息
17. Response.Write("MAC地址: " + GetMacAddress());
18. }
19. /// <summary>
20. /// 获取网卡地址信息
21. /// </summary>
22. /// <returns></returns>
23.stringGetMacAddress()
24. {
25.try
26. {
27.stringmac = "";
28. ManagementClass mc =newManagementClass("Win32_NetworkAdapterConfiguration");
29. ManagementObjectCollection moc = mc.GetInstances();
30.foreach(ManagementObject moinmoc)
31. {
32.if((bool)mo["IPEnabled"] ==true)
33. {
34. mac = mo["MacAddress"].ToString();
35.break;
36. }
37. }
38. moc =null;
39. mc =null;
40.returnmac;
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.usingSystem;
2.usingSystem.Collections.Generic;
3.usingSystem.Linq;
4.usingSystem.Web;
5.usingSystem.Web.UI;
6.usingSystem.Web.UI.WebControls;
7. //导入命名空间
8.usingSystem.Runtime.InteropServices;
9.namespaceWebApp
10. {
11.publicpartialclassWebForm2 : System.Web.UI.Page
12. {
13. [DllImport("Iphlpapi.dll")]
14.privatestaticexternintSendARP(Int32 dest, Int32 host,refInt64 mac,refInt32 length);
15. [DllImport("Ws2_32.dll")]
16.privatestaticexternInt32 inet_addr(stringip);
17.privatevoidPage_Load(objectsender, System.EventArgs e)
18. {
19.try
20. {
21.stringuserip = Request.UserHostAddress;
22.stringstrClientIP = Request.UserHostAddress.ToString().Trim();
23. Int32 ldest = inet_addr(strClientIP); //目的地的ip
24. Int64 macinfo =newInt64();
25. Int32 len = 6;
26.intres = SendARP(ldest, 0,refmacinfo,reflen);
27.stringmac_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.stringmac_dest = "";
45.for(inti = 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" "/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3.<htmlxmlns="/1999/xhtml">
4.<headrunat="server">
5.<title></title>
6.<mce:scriptlanguage="javascript"><!--
7. document.write("<OBJECTid='locator' classid='CLSID:76A64158-CB41-11D1-8B02-00600806D9B6' VIEWASTEXT></OBJECT>");
8. document.write("<OBJECTid=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.<formid="form1" runat="server">
60.<div>
61.
62.<asp:ButtonID="Button1" runat="server" οnclick="Button1_Click" Text="Button"/>
63.
64.</div>
65.</form>
66.</body>
67.</html>
后台代码:(WebForm3.aspx.cs)
[c-sharp]view plaincopy
1.usingSystem;
2.usingSystem.Collections.Generic;
3.usingSystem.Linq;
4.usingSystem.Web;
5.usingSystem.Web.UI;
6.usingSystem.Web.UI.WebControls;
7.namespaceWebApp
8. {
9.publicpartialclassWebForm3 : System.Web.UI.Page
10. {
11.protectedvoidPage_Load(objectsender, EventArgs e)
12. {
13. }
14. /// <summary>
15. /// 获取mac地址
16. /// </summary>
17. /// <returns></returns>
18.publicstringGetMACs()
19. {
20.try
21. {
22.returnRequest.Form["txtMAC"].ToString();
23. }
24.catch
25. {
26.return"";
27. }
28. }
29.protectedvoidButton1_Click(objectsender, EventArgs e)
30. {
31. Response.Write(GetMACs());
32. }
33. }
34. }
运行提示:
启用未标记为可安全执行脚本的ActiveX
第一步:工具->Internet选项
第二步:安全->Internet->自定义级别
第三步:启用“对未标记为可安全执行脚本的ActiveX控件初始化并执行”,如下图所示:
运行结果:
总结:感觉第一种方法还是方便,第二种方法获得不到服务器的MAC地址,第三种方法还需设置,两个字“不爽”。
参考://show-6092-1.aspx