如何使用c#asp.net获取客户端机器的macadress? [英] how to get macadress of client machine using c# asp.net?

查看:89
本文介绍了如何使用c#asp.net获取客户端机器的macadress?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在谷歌搜索但我无法获得完美的代码。我尝试了一些代码但我无法跟踪客户端mac地址我正在获取服务器mac地址。在本地它的工作正常,但当我在服务器上传时,我没有得到我机器的mac地址。



这里有一些代码我正在使用



使用System.Net.NetworkInformation;



 NetworkInterface [] nics = NetworkInterface.GetAllNetworkInterfaces() ; 
Label1.Text = nics [ 0 ]。GetPhysicalAddress()。ToString();





如果其他解决方案请提供给我..

解决方案

Web应用程序无法访问客户端系统。这非常好。如果某些网站可以检测到您的MAC地址,您会信任它们吗?



-SA


查看以前的问题和答案:如何获取客户端mac地址 [ ^ ]


您需要在代码中导入System.Net命名空间才能生效。这将支持IPv4和IPv6。



公共字符串GetMACAddress()

{

NetworkInterface [] nics = NetworkInterface.GetAllNetworkInterfaces();

String sMacAddress = string.Empty;

foreach(nics中的NetworkInterface适配器)

{

if(sMacAddress == String.Empty)//仅从第一张卡返回MAC地址

{

IPInterfaceProperties properties = adapter.GetIPProperties();

sMacAddress = adapter.GetPhysicalAddress()。ToString();

}

}返回sMacAddress;

}































要获得客户端MAC地址,我们可以依赖JavaScript和Active X控件Microsoft.It仅适用于IE,如果是Active X.启用IE。由于Firefox不提供ActiveXObject,它不能与firefox一起使用,并且在IE中工作正常。 

此脚本仅适用于IE:

< script language = < span class =code-keyword> javascript type = text / 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 ='< table border = 0 < span class =code-attribute> cellPadding = 5px cellSpacing = 1px bgColor = #CCCCCC > ' ;
output = output +'< tr bgColor = #EAEAEA > < td < span class =code-keyword>> 标题< / td > < td > MACAddress < / td > < / tr > ';
while(!e.atEnd()){
e.moveNext();
var p = e.item();
如果(!p)继续;
output = output +'< tr bgColor = #FFFFFF > ';
输出=输出+'< td > '+ p.Caption; +'< / td > ;
输出=输出+'< td > '+ p.MACAddress +'< / td > ';
输出=输出+'< / tr > ';
}
输出=输出+'< / table > ';
document.getElementById(box)。innerHTML = output;
}
< / script >


I search it on google but i can't get perfect code. i try some codes but i am not able track the client mac address i am getting server mac adress. in local its works fine but when i upload on server i am not getting mac adress of my machine.

here some code i am using

using System.Net.NetworkInformation;

NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
              Label1.Text = nics[0].GetPhysicalAddress().ToString();



if other solution please provide me..

解决方案

Web applications don't have access to client systems. And this is very good. If some sites could detect your MAC address, would you ever trust them?

—SA


check this previous question and answers: how to get client mac address[^]


You need to import the System.Net namespace in your code for this to work. This will support IPv4 and IPv6.

public string GetMACAddress()
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
String sMacAddress = string.Empty;
foreach (NetworkInterface adapter in nics)
{
if (sMacAddress == String.Empty)// only return MAC Address from first card
{
IPInterfaceProperties properties = adapter.GetIPProperties();
sMacAddress = adapter.GetPhysicalAddress().ToString();
}
} return sMacAddress;
}















To get the client MAC address only way we can rely on JavaScript and Active X control of Microsoft.It is only work in IE if Active X enable for IE. As the ActiveXObject is not available with the Firefox, its not working with the firefox and is working fine in IE.

This script is for IE only:

<script language="javascript" type="text/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>


这篇关于如何使用c#asp.net获取客户端机器的macadress?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆