【发布时间】: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