Search code examples
asp.netiisprintingimpersonation

Network Connected Printer not available in ASP.Net Application


I have an ASP.Net 4.0 web application running on IIS 7.5, I can get the list of installed printers using System.Drawing.Printing.PrinterSettings.InstalledPrinters command and assign each report in the application to any printer listed above!

There might be some Network Connected Printers on the server all set with full privilege to the application's user account, everything works perfect until the application's user account logs off the windows, at this point System.Drawing.Printing.PrinterSettings.InstalledPrinters returns only local printers, no Network Connected Printers are listed!

I've tried to impersonate the ASP.Net process to the user with access to Network Printers in following ways, yet no success:

  • I configured the application pool Process Model to run as a specific user identity.
  • I impersonated the Application's identity to specific user in Web.Config:

    <identity impersonate="true" userName="user" password="pass"/>

  • And finally implemented impersonation in code using advapi32.dll API

In all of the methods above, the WindowsIdentity returns the true username when printing:

System.Security.Principal.WindowsIdentity.GetCurrent().Name

But it looks like that impersonation is not the issue here, the moment the user logs off, all Network Connected Printers are gone!

Does anybody know how to resolve this? Is there any way to access Network Connected Printers even when the user is not logged in?


Solution

  • Network Connected Printers, Shared Folders and Map Drives are only accessible when user logs into the OS, they are barely a shortcut to some network resources, all disconnected and are inaccessible when the corresponding user logs off!

    Using System.Printing.PrintServer and the GetPrintQueues method, you can get the collection of print queues that the print server hosts:

    PrintServerUNCName is the UNC name of the corresponding server, as in \\MyServerName

    var printers = new PrintServer("PrintServerUNCName").GetPrintQueues()
        .Where(t =>
        {
            try { return t.IsShared && !t.IsNotAvailable; }
            catch { return false; }
        })
        .Select(t => t.FullName)
        .ToArray();