如何在所有浏览器中获取客户端mac地址 [英] how to get client mac address in all browser
本文介绍了如何在所有浏览器中获取客户端mac地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
如何获取客户端mac地址。在这里我使用下面的脚本,但它的工作原理仅适用于所有浏览器。这个代码我托管在服务器中但是也没有检索任何值,即使也是。
脚本: -
function callme(){
var macAddress = ;
var ipAddress = ;
var computerName = ;
var wmi = GetObject( winmgmts: {impersonationLevel =冒充}跨度>);
e = new 枚举器(wmi.ExecQuery( SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True));
for (;!e.atEnd(); e.moveNext()){
var s = e.item();
macAddress = s.MACAddress;
ipAddress = s.IPAddress( 0 );
computerName = s.DNSHostName;
alert(macAddress);
document.getElementById(' ctl00_cplhControlPanel_macAddress')。 value = macAddress;
}
}
这里函数调用onclientevent。请在所有浏览器中帮助我
解决方案
您不能 - MAC地址不会超出路由器,因此您必须在客户端上运行代码。我知道的唯一方法是IE特定的。
说实话,它可能无法帮助你:MAC地址不是唯一的(它们只需要在LAN网段中是唯一的)并且它们很容易被欺骗。因此,如果你打算使用MAC进行身份识别/授权,我建议你忘掉它并安装一个cookie。
我在这里使用了它
(使用System.Runtime.InteropServices;)
[DllImport(Iphlpapi.dll)]
private static extern int SendARP(Int32 dest,Int32主机,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)
{
try
{
string userip = Request.UserHostAddress;
string strClientIP = Request.UserHostAddress.ToString()。Trim();
Int32 ldest = inet_addr(strClientIP);
Int32 lhost = inet_addr();
Int64 macinfo = new Int64();
Int32 len = 6;
int res = SendARP(ldest,0,ref macinfo,ref len);
string mac_src = macinfo.ToString(X);
i f(mac_src ==0)
{
if(userip ==127.0.0.1)
Response.Write(访问过Localhost!);
else
Response.Write(IP from+ userip ++
);
返回;
}
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));
}
}
}
Response.Write(welcome+ userip +
+,mac地址是+ mac_dest +。
+
);
}
catch(例外错误)
{
Response.Write(err.Message);
}
} < BR>搜索结果
hi to all,
How to get client mac address. here i using this below script but its works only in ie not in all browser. and this code i hosted in server but not retrieve any value even in ie also.
SCRIPTS:-
function callme() {
var macAddress = "";
var ipAddress = "";
var computerName = "";
var wmi = GetObject("winmgmts:{impersonationLevel=impersonate}");
e = new Enumerator(wmi.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True"));
for (; !e.atEnd() ; e.moveNext()) {
var s = e.item();
macAddress = s.MACAddress;
ipAddress = s.IPAddress(0);
computerName = s.DNSHostName;
alert(macAddress);
document.getElementById('ctl00_cplhControlPanel_macAddress').value = macAddress;
}
}
here this function call in onclientevent. kindly help me in all browser
解决方案
You can't - MAC addresses do not travel beyond the router, so you have to run code on the client. And the only ways to do that I know of are IE specific.
And to be honest, it probably won't help you: MAC addresses aren't unique (they only have to be unique within a LAN segment) and they are very easy to spoof. So if you are planning on using MAC for identification / authorization, I would recommend you forget it and install a cookie instead.
i got it here i used
(using System.Runtime.InteropServices;)
[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)
{
try
{
string userip = Request.UserHostAddress;
string strClientIP = Request.UserHostAddress.ToString().Trim();
Int32 ldest = inet_addr(strClientIP);
Int32 lhost = inet_addr("");
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")
Response.Write("visited Localhost!");
else
Response.Write("the IP from" + userip + "" + "
");
return;
}
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));
}
}
}
Response.Write("welcome" + userip + "
" + ",the mac address is" + mac_dest + "."
+ "
");
}
catch (Exception err)
{
Response.Write(err.Message);
}
}
这篇关于如何在所有浏览器中获取客户端mac地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文