伙计我在下面的代码中尝试在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.
答案 0 :(得分:1)
正如Henk在您的代码中所说,您正在获取服务器MAC地址。在本地计算机上,它只能用于本地计算机是客户端和服务器,并且运行具有更高权限的代码。您可以使用某些客户端脚本获取客户端MAC地址。在这里你可以找到关于它的讨论&#39; MAC addresses in JavaScript&#39;
答案 1 :(得分:-2)
知道了。为此,我们需要在web.config中为我们的应用程序提供信任级别。出于这个原因,我们需要在web.config文件的system.web中添加以下代码。
<trust level="Full" originUrl=""/>
这真的有助于所有人。