System.Management access denied
I was using some code to try to count the number of processors in .NET 2.0:
internal static int GetNumberOfProcessors()
{
List<str开发者_如何学运维ing> list = new List<string>();
ManagementClass mgmt = new ManagementClass("Win32_Processor");
foreach (ManagementObject obj in mgmt.GetInstances())
{
string item = obj.Properties["SocketDesignation"].Value.ToString();
if (!list.Contains(item))
{
list.Add(item);
}
}
return list.Count;
}
and it blew up like this:
[ManagementException: Access denied ]
System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode) +377984
System.Management.ManagementScope.InitializeGuts(Object o) +654
System.Management.ManagementScope.Initialize() +162
System.Management.ManagementObject.Initialize(Boolean getObject) +492
System.Management.ManagementClass.GetInstances(EnumerationOptions options) +122
System.Management.ManagementClass.GetInstances() +6
This code runs fine locally on cassini, but blows up on our beta server. Anyone have any idea what to do about this?
Ok, I hate answering my own question, but I found this: http://support.microsoft.com/kb/317012 and it does seem to work.
You have to specifically modify your CIMV2 permissions, and I had to do it for the specific user, not ASPNET. Once this is on, you can use the System.Management code to count processors etc.
I really don't like how obscure this was. Microsoft needs to update those exceptions. When the System.Management class fails like that it should give detailed info on which permissions need activated for the code to work.
Looks like a permissions issue.
If it runs fine locally (under cassini), it would be running under your credentials, which probably have local admin rights, hence the right permissions to access this data.
Make sure the code is running as a user that has sufficient permissions to get this data (if running IIS 7 and above look at the application pool identity - that would be the account running the code).
Gosh, still encounter the same issue using Aspose.Email (domain email address validation).
Here is my error message:
System.Management.ManagementException: Access denied at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode) at System.Management.ManagementScope.InitializeGuts(Object o) at System.Management.ManagementScope.Initialize() at System.Management.ManagementObjectSearcher.Initialize() at System.Management.ManagementObjectSearcher.Get() at .() at .() at .(String ) at Aspose.Email.Verify.EmailValidator.(String , Int32 ) at Aspose.Email.Verify.EmailValidator.(String , MailAddress , String[]& )
And my solution for my ASP.NET application is wrap the code with
using (HostingEnvironment.Impersonate())
{
...
}
精彩评论