【问题标题】:Error in getting mac address of client machine获取客户端机器的mac地址时出错
【发布时间】:2014-10-04 02:06:27
【问题描述】:

伙计们,我在尝试在 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);
        }
    }
}

错误是这样的:

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.

【问题讨论】:

  • 在这两种情况下,它都是 Server 的 MAC 地址。而在真实服务器上,您甚至没有权限阅读。
  • 但我想获取客户端机器的mac地址而不是服务器机器
  • 你想要的并不总是你得到的。检查 pen2 的答案。

标签: c# mac-address


【解决方案1】:

知道了。为此,我们需要在 web.config 中为我们的应用程序提供信任级别因此,我们需要在 web.config 文件的 system.web 中添加以下代码。

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

这真的很有帮助。

【讨论】:

  • 您可能没有得到它,所以我会重复一遍:即使您通过设置信任级别设置了适当的权限,您也不会使用您的代码获取客户端 MAC 地址。您粘贴的代码只会为您提供服务器 MAC 地址。
  • 你似乎并不专业和乐于助人......不需要你无用的建议。
  • 您无法从 ASP.NET 获取客户端的 MAC 地址。如果您有某种方式在客户端上运行代码(例如使用 Silverlight),那么它应该能够获得它。
【解决方案2】:

正如 Henk 在您的代码中所说,您正在获取服务器 MAC 地址。在您的本地机器上,它只能工作,因为您的本地机器是客户端和服务器,并且您以更高的权限运行您的代码。您可以使用一些客户端脚本获取客户端 MAC 地址。在这里你可以找到一个关于它的问题'MAC addresses in JavaScript'

【讨论】:

    猜你喜欢
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 2011-07-22
    相关资源
    最近更新 更多