首页 新闻 搜索 专区 学院

asp.net获取客户端的MAC地址

0
悬赏园豆:10 [已解决问题] 解决于 2011-10-25 22:05

asp.net网站需要获取客户端的MAC地址。用js什么的都行,但是得保证ie和FF都能用,其他浏览器尽量兼容。能实现吗?有用过的吗?

阿@杜的主页 阿@杜 | 初学一级 | 园豆:88
提问于:2011-10-05 19:08
< >
分享
最佳答案
0

需要通过ActiveX获取。

IE下的代码如下(代码来源):

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Getting MAC Address From Javascript(IE Only)</title>

<script language="javascript">
function showMacAddress(){

var obj =new ActiveXObject("WbemScripting.SWbemLocator");
var s = obj.ConnectServer(".");
var properties = s.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
var e =new Enumerator (properties);


var output;
output
='<table border="0" cellPadding="5px" cellSpacing="1px" bgColor="#CCCCCC">';
output
=output +'<tr bgColor="#EAEAEA"><td>Caption</td><td>MACAddress</td></tr>';
while(!e.atEnd())

{
e.moveNext();
var p = e.item ();
if(!p) continue;
output
=output +'<tr bgColor="#FFFFFF">';
output
=output +'<td>'+ p.Caption; +'</td>';
output
=output +'<td>'+ p.MACAddress +'</td>';
output
=output +'</tr>';
}

output
=output +'</table>';
document.getElementById(
"box").innerHTML=output;
}
</script>

</head>
<body>
<input type="button" value="Show MAC Address" onclick="showMacAddress()"/>

<div id="box">
</div>
</body>
</html>

FireFox中需要安装插件MediaWrap,让FireFox支持ActiveX。(参考自How to get MAC Address in Firefox using Javascript?

收获园豆:10
dudu | 高人七级 |园豆:39014 | 2011-10-05 20:34
其他回答(2)
0

mark!楼上的代码在IE中可以。

水晶途途 | 园豆:1434 (小虾三级) | 2011-10-05 23:49
0

用.net实现

代码如下:

    

public static string GetIP()
        {
            string HostName = Dns.GetHostName(); //get Hostname
            IPHostEntry IpEntry = Dns.GetHostEntry(HostName); //get IP
            string strIPAddr = IpEntry.AddressList[0].ToString();
            return (strIPAddr);
        }

        /// <summary>
        /// get MacAddress
        /// </summary>
        /// <returns></returns>
        public static string GetMac()
        {
            string MacAddress = String.Empty;
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");

            ManagementObjectCollection moc = mc.GetInstances();

            foreach (ManagementObject mo in moc)
            {

                if ((bool)mo["IPEnabled"] == true)
                {

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

                    break;

                }

            }
            moc = null;
            mc = null;
            MacAddress = MacAddress.Replace(":", "");
            return MacAddress;
        }

--中庸-- | 园豆:408 (菜鸟二级) | 2011-10-13 21:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册