获取MAC地址的几种方式
.NET 后台中
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Runtime.InteropServices; namespace WebApplication3 { public partial class a1 : System.Web.UI.Page { [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); protected void Page_Load(object sender, EventArgs e) { Response.Write(GetClientMAC()); Response.Write("<br>"); Response.Write(gett()); return; } ///<summary> ///得到客户端ip ///</summary> ///<returns>ip</returns> private static string GetClientIP() { string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (null == result || result == String.Empty) { result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } if (null == result || result == String.Empty) { result = HttpContext.Current.Request.UserHostAddress; } return result; } ///<summary> /// 利用客户端ip得到客户端mac ///</summary> ///<param name="remoteip">客户端ip</param> ///<returns>int16类型的mac</returns> static private Int64 getremotemac(string remoteip) { Int32 ldest = inet_addr(remoteip); try { Int64 macinfo = new Int64(); Int32 len = 6; int res = SendARP(ldest, 0, ref macinfo, ref len); return macinfo; } catch (Exception err) { throw err; } return 0; } ///<summary> /// int64类型的mac转换成正确的客户端mac ///</summary> ///<returns>mac</returns> public static string GetClientMAC() { Int64 macid = getremotemac(GetClientIP()); if (macid == 0) return "0"; string beforeMacAddr = Convert.ToString(macid, 16); string endMacAddr = ""; string[] macArray = new string[6]; for (int i = 0; i < 6; i++) { macArray[i] = beforeMacAddr.Substring(i * 2, 2); } for (int i = 0; i < 6; i++) { endMacAddr += macArray[5 - i] + "-"; } endMacAddr = endMacAddr.Substring(0, endMacAddr.Length - 1); endMacAddr = endMacAddr.ToUpper(); return endMacAddr; } public static string gett() { string userip = HttpContext.Current.Request.UserHostAddress; string strClientIP = HttpContext.Current.Request.UserHostAddress.ToString().Trim(); Int32 ldest = inet_addr(strClientIP); //目的地的ip Int32 lhost = inet_addr(""); //本地服务器的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") return "正在访问Localhost!"; else return "欢迎来自IP为" + userip + "的朋友!" + "<br>"; } 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)); } } } return "欢迎来自IP为" + userip + "<br>" + ",MAC地址为" + mac_dest + "的朋友!" + "<br>"; } } }
JS 中(限IE安装插件之后):
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="a1.aspx.cs" Inherits="WebApplication3.a1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <div style="display:none"> <OBJECT id=locator classid=CLSID:76A64158-CB41-11D1-8B02-00600806D9B6 VIEWASTEXT></OBJECT> <OBJECT id=foo classid=CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223></OBJECT> </div> <form id="formfoo" name="formfoo" runat="server"> <div> <br/>MAC地址:<input type="text" name="macAddress">*仅IE浏览器下 显示,如果不显示 IE浏览器需要做设置允许未标记的activex <br/>IP地址:<input type="text" name="ipAddress"> <br/>主机名:<input type="text" name="hostName"> </div> <script language="javascript"> var sMacAddr=""; var sIPAddr=""; var sDNSName=""; var service = locator.ConnectServer(); service.Security_.ImpersonationLevel=3; service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration'); </script> <script FOR="foo" EVENT="OnObjectReady(objObject,objAsyncContext)" LANGUAGE="JScript"> if(objObject.IPEnabled != null && objObject.IPEnabled != "undefined" && objObject.IPEnabled == true){ if(objObject.IPEnabled && objObject.IPAddress(0) !=null && objObject.IPAddress(0) != "undefined") sIPAddr = objObject.IPAddress(0); if(objObject.MACAddress != null &&objObject.MACAddress != "undefined") sMacAddr = objObject.MACAddress; if(objObject.DNSHostName != null &&objObject.DNSHostName != "undefined") sDNSName = objObject.DNSHostName; } </script> <script FOR="foo" EVENT="OnCompleted(hResult,pErrorObject, pAsyncContext)" LANGUAGE="JScript"> formfoo.macAddress.value=sMacAddr; formfoo.ipAddress.value=sIPAddr; formfoo.hostName.value=sDNSName; </script> </form> </body> </html>