简体   繁体   中英

Permission denied while querying Win32_Printer in ASP.NET on IIS using Application Pool Identity

I am querying Win32_Printer in ASP.net code on IIS using Application Pool Identity (Windows 2016 Server machine). On one of the server I am getting Access denied error. The code work fine on other servers. I compared the two servers but unable to find difference in permissions. What permissions do I need to give to IIS App Pool Identity user to make this call? Here is the code snippet.


        public static List<string> GetPrinterNames()
    {
        var query = new System.Management.ObjectQuery("SELECT * FROM Win32_Printer");
        var searcher = new System.Management.ManagementObjectSearcher(query);
        var printerList = new List<string>();
        foreach (var dummyPrinterObject in searcher.Get())
        {
            printerList.Add(dummyPrinterObject["DeviceID"] as string);
        }

        return printerList;
    }

Use

public static System.Drawing.Printing.PrinterSettings.StringCollection InstalledPrinters { get; }

The example is shown at this documentation link :

private void PopulateInstalledPrintersCombo()
{
    // Add list of installed printers found to the combo box.
    // The pkInstalledPrinters string will be used to provide the display string.
    String pkInstalledPrinters;
    for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++){
        pkInstalledPrinters = PrinterSettings.InstalledPrinters[i];
        comboInstalledPrinters.Items.Add(pkInstalledPrinters);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM