获取机器的mac地址c#

0 c# asp.net asp.net-mvc asp.net-mvc-3

我正在研究ASP.NET MVC应用程序,我想获得用户的MAC地址.经过一番研究,我发现了这段代码:

string GetMacAddress()
{
    string macAddresses = "";
    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
    {
        if (nic.NetworkInterfaceType != NetworkInterfaceType.Ethernet)
            continue;
        if (nic.OperationalStatus == OperationalStatus.Up)
        {
            macAddresses += nic.GetPhysicalAddress().ToString();
            break;
        }
    }

    return macAddresses;
}
Run Code Online (Sandbox Code Playgroud)

没有错误,但我总是得到一个空地址.有谁知道如何解决这个问题?

Dar*_*rov 8

我正在使用mvc .net应用程序,我想得到用户的mac地址.

你可以忘掉它,这是不可能的.您只能获得浏览您网站的客户端的IP地址.

实际上有一种方法可以实现它.在视图中添加以下内容:

@using (Html.BeginForm())
{
    @Html.Label("Mac", "Please enter your MAC address")
    @Html.TextBox("Mac")
    <button type="submit">OK</button>
}
Run Code Online (Sandbox Code Playgroud)

并在相应的控制器动作内:

[HttpPost]
public ActionResult SomeAction(string mac)
{
    // if the user was kind enough to provide you his MAC address
    // you could read it from the mac argument
    // Of course he could have entered any MAC address he likes and
    // you have no way of verifying that
    ...
}
Run Code Online (Sandbox Code Playgroud)

另一种可能性是开发ActiveX控件(仅在IE下工作)并且用户可以在他的浏览器中安装,该浏览器可以检索其NIC的MAC地址并将其发送到服务器.

  • @FilipEkberg,我很高兴这是你的答案.:) (3认同)