C# 获取客户端计算机的mac地址时出错

C# 获取客户端计算机的mac地址时出错,c#,mac-address,C#,Mac Address,伙计们,在asp.net c#中尝试获取客户端计算机的mac地址时,我在下面的代码中遇到错误。当我在本地计算机上运行相同的代码时,它工作正常,但当我将相同的代码上载到服务器时,我得到如下所示的错误 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System

伙计们,在asp.net c#中尝试获取客户端计算机的mac地址时,我在下面的代码中遇到错误。当我在本地计算机上运行相同的代码时,它工作正常,但当我将相同的代码上载到服务器时,我得到如下所示的错误

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Security.Policy;
using System.Management;
using System.Management.Instrumentation;
public partial class GetMac : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string id = "";
        ManagementObjectSearcher query = null;
        ManagementObjectCollection queryCollection = null;

        try
        {
            query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");
            queryCollection = query.Get();
            foreach (ManagementObject mo in queryCollection)
            {
                if (mo["MacAddress"] != null)
                {
                    id = mo["MacAddress"].ToString();
                    Response.Write(id+"<br/>");

                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Source);
            Response.Write(ex.Message);
        }
    }
}

正如Henk在代码中所说,您将获得服务器MAC地址。在您的本地计算机上,它只能工作,因为您的本地计算机是客户机和服务器,并且您以更高的权限运行代码。您可以使用一些客户端脚本获取客户端MAC地址。在这里你可以找到一个关于它的讨论,明白了吗。为此,我们需要在web.config中为应用程序提供信任级别,因此我们需要在web.config文件的system.web中添加以下代码

<trust level="Full" originUrl=""/>


这真的很有帮助。

在这两种情况下,它都是服务器的MAC地址。而在真正的服务器上,你甚至连读这些的权限都没有。但是我想要的是客户端机器的mac地址,而不是服务器机器。你想要的并不总是你得到的。检查pen2的答案。可能有一小部分机会您没有得到它,所以我将重复:即使您通过设置信任级别设置了适当的特权,您也无法使用代码获得客户端MAC地址。你粘贴的代码只会给你服务器的MAC地址。你似乎不专业,也没有帮助…不需要你无用的建议。你无法从ASP.NET获取客户端的MAC地址。如果您有办法在客户机上运行代码(例如,使用Silverlight),那么它应该能够获得它。
<trust level="Full" originUrl=""/>