获取客户端机器mac地址时出错
本文关键字:出错 地址 mac 客户端 机器 获取 | 更新日期: 2023-09-27 18:17:29
伙计们,我在下面的代码中得到错误,试图在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.
正如Henk在你的代码中所说,你正在获得服务器的MAC地址。在您的本地机器上,它只工作,因为您的本地机器是客户端和服务器,您运行您的代码具有更高的特权。您可以使用一些客户端脚本获取客户端MAC地址。在这里你可以找到关于它的讨论' JavaScript中的MAC地址'
明白了。为此,我们需要在web中为我们的应用程序赋予信任级别。为此,我们需要在系统中添加以下代码。网络中的网络。配置文件。
<trust level="Full" originUrl=""/>