简体   繁体   English

获取客户端机器的mac地址时出错

[英]Error in getting mac address of client machine

Guys I am getting error in the following code in trying to get mac address of client machine in asp.net c#.When I run same code on local machine it works perfectly but when I upload same code to server I get the error as shown below.伙计们,我在尝试在 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);
        }
    }
}

Error is like this:错误是这样的:

App_Web_klgxzt4kAttempt by security transparent method 'GetMac.Page_Load(System.Object, System.EventArgs)' to access security critical method 'System.Management.ManagementObjectSearcher..ctor(System.String)' failed. Assembly 'App_Web_klgxzt4k, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is partially trusted, which causes the CLR to make it entirely security transparent regardless of any transparency annotations in the assembly itself. In order to access security critical code, this assembly must be fully trusted.

As Henk said in your code you are getting server MAC address.正如 Henk 在您的代码中所说,您正在获取服务器 MAC 地址。 On your local machine it works only because your local machine is client AND server and you run your code with higher priviledge.在您的本地机器上,它只工作是因为您的本地机器是客户端和服务器,并且您以更高的特权运行您的代码。 You can get client MAC address using some client side script.您可以使用一些客户端脚本获取客户端 MAC 地址。 Here you can find a discution about it ' MAC addresses in JavaScript '在这里您可以找到有关它的讨论“JavaScript 中MAC 地址

Got it.知道了。 For this we need to give trust level to our application in web.config For that reason we need to add following code in system.web in web.config file.为此,我们需要在 web.config 中为我们的应用程序提供信任级别,因此我们需要在 web.config 文件中的 system.web 中添加以下代码。

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

It really helps thx all.这真的很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM