0

The principal permission succeeds but I still have System.Management.ManagementException: Access denied

public class HardDriveTemp
{
    private const byte _tempAttr = 194;
    private ManagementObjectSearcher searcher;

    public HardDriveTemp()
    {
        AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
        searcher = new ManagementObjectSearcher(@"root\WMI", "SELECT * FROM MSStorageDriver_ATAPISmartData");
    }

    [PrincipalPermission(SecurityAction.Demand, Role = @"Administrator", Authenticated = false)]
    public List<string> GetDriveTemp()
    {
        var tempList = new List<string>();
        foreach (var q in searcher.Get()) //fails here
        {
            var arrVendorSpecific = (byte[])q.GetPropertyValue("VendorSpecific");
            int tempIndex = Array.IndexOf(arrVendorSpecific, _tempAttr);
            tempList.Add(arrVendorSpecific[tempIndex + 5].ToString());
        }
        return tempList;

    }

And I run the function in a test:

    [TestMethod]
    public void GetDriveTemp()
    {
        var sut = new HardDriveTemp();
        var results = sut.GetDriveTemp();
        Console.WriteLine(string.Join("\n", results));
    }

I tried with: Administrator, Administrators, MACHINENAME\localuser (user of WindowsIdentity.GetCurrent()) without success. I also granted read access to WMI to localuser from MMC, but it did not change a thing.

What can be done ?

Soleil
  • 6,404
  • 5
  • 41
  • 61

0 Answers0