System.Management.ManagementException: 访问遭到拒绝的解决方案 dodo

解决方案: 原理:页面(与IIS有关)运行的权限低于DLL运行的权限,估计是微软考虑到什么黑客会利用此漏洞而设计的。只要在运行页面前把这个值获取就没问题。 结果整理了一下,完整代码如下:  /// <summary>         /// 获取CPUID         /// </summary>         /// <returns></returns>         public static string GetCpuId()         {             string cpuId = null;             ManagementClass mClass = new ManagementClass("Win32_Processor");             try             {                 ManagementObjectCollection moc = mClass.GetInstances();

                foreach (ManagementObject mo in moc)                 {                     cpuId = mo.Properties["ProcessorId"].Value.ToString();                     break;                 }             }             catch (Exception ex)             {                 cpuId =ex.ToString();             }             if (System.Web.HttpContext.Current.Application["CPUID"] == null)             {                 System.Web.HttpContext.Current.Application.Lock();                 System.Web.HttpContext.Current.Application["CPUID"] = cpuId;                 System.Web.HttpContext.Current.Application.UnLock();             }             return cpuId;         }
在页面调用CPUID值: System.Web.HttpContext.Current.Application["CPUID"].ToString(); 运行的前提是必须把这个代码在页面执行前运行,如加在 httpModules 里程序一开始就先执行这个代码。
最后贴上运行效果:
--------------页面调用前开始-------------- CPUID值:BFEBFBFF00000F29 --------------页面调用前结束--------------
--------------页面调

用开始-------------- CPUID值:System.Management.ManagementException: 访问遭到拒绝 在 System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode) 在 System.Management.ManagementScope.InitializeGuts(Object o) 在 System.Management.ManagementScope.Initialize() 在 System.Management.ManagementObject.Initialize(Boolean getObject) 在 System.Management.ManagementClass.GetInstances(EnumerationOptions options) 在 System.Management.ManagementClass.GetInstances() 在 ManagementTest.Class1.GetCpuId() --------------页面调用结束--------------

原文地址:https://www.cnblogs.com/zgqys1980/p/3108070.html