问题
I am developing a Web API that is deployed in IIS 8 on Windows Server 2012 R2. It requires that documents be printed from the Server to a network printer.
I am developing on a RDP session which has a redirected printer, this printer is also set as my default printer.
I have created a dummy proof of concept as a Console Application to userstand what is necessary to make this work.
The dummy console uses the following code to find printers
var ps = new PrinterSettings();
foreach(var p in PrinterSettings.InstalledPrinters)
{
var name = p as string;
Console.WriteLine(name);
}
When running as a Console application this picks up the default redirected printer and works.
I have moved this code to the Web API and done the follwoing
- set the Application Pool of the site to run under my account (for test purposes)
- Installed the internet Printing Role
- Installed the Internet Print Client feature
After doing this the default printer is no longer in the list of installed printers and there is no default available.
What additional permissions/changes do I have to make in order that the IIS hosted site can see the redirected printer?
回答1:
The account that IIS is running under needs to have access to the print queue you want to use. By default, it doesn't. Whoever maintains the server needs to grant the account the needed permissions.
Note that this won't be the "redirected printer" from your RDP session. The printer needs to be configured on the IIS server, and the account IIS is running as needs to have access to it.
The RDP session is isolated. I don't believe there's a way (at least not a clean, documented stable way) to give a server process access to a resource in an RDP session, however you can certainly give it it's own access using the normal printer configuration process.
From your server-side code, you would open the printer normally and do whatever you want, because it would just be a normal printer.
回答2:
Just an idea: Did you check "Load User profile" ? See
Security exceptions in ASP.NET and Load User Profile option in IIS 7.5
and
https://blogs.msdn.microsoft.com/vijaysk/2009/03/08/iis-7-tip-3-you-can-now-load-the-user-profile-of-the-application-pool-identity/
I am not an IIS expert, but had some problem many years ago with scheduled tasks not loading the user profile.
来源:https://stackoverflow.com/questions/51190741/c-sharp-web-api-cannot-find-redirected-printer