System.Management.ManagementException: 訪問遭到拒絕的解決方案


解決方案: 原理:頁面(與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() --------------頁面調用結束--------------


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM