Network Printers and Web Services

B

Bill Gates

Hello,

I am having a little trouble accessing a list of printers on our Network through a web service...

I am using the PrinterSettings.InstalledPrinters to access a list of printers installed on the local machine which works fine when using a test windows application on the server and calling the function directly.

However if the test application first calls a Web Service which then calls the function to retrieve a list of prints I only get a small sub-set of printers installed on the machine.

I believe this problem is related to permissions for the user under which the Web Service runs but I am not sure, does anyone have any ideas on this.

Regards,

Bill
 
N

Nicholas Paldino [.NET/C# MVP]

Mr. Gates,

First off, let me say what an honor it is that you are gracing our
forums with your presence =P

Anyways, it most likely is a permissions issue like you stated. By
default, the service is running under the ASPNET local account. You will
have to change the web.config file for the directory the service is in to
specify the user to run the service under.

Or, you could look at the documentation of the Impersonate method on the
WindowsIdentity class, and see how to impersonate a user for a section of
code (instead of all code in a directory).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello,

I am having a little trouble accessing a list of printers on our Network
through a web service...

I am using the PrinterSettings.InstalledPrinters to access a list of
printers installed on the local machine which works fine when using a test
windows application on the server and calling the function directly.

However if the test application first calls a Web Service which then calls
the function to retrieve a list of prints I only get a small sub-set of
printers installed on the machine.

I believe this problem is related to permissions for the user under which
the Web Service runs but I am not sure, does anyone have any ideas on this.

Regards,

Bill
 
W

Willy Denoyette [MVP]

1. You can't use the printer classes from a service (a webservice runs in the context of a windows service)
2. You can't enumerate Printers from a service.
Printer devices are bound to the context of an interactive user, their attributes are strored in the profile (user hive) of the current user (see HKCU).
A web service runs in the context of a non interactive user (ASPNET by default), so there is no specific user profile loaded for webservices or any other service for that matter (aspnet has no user profile).

If printing is required from a service (but you shouldn't), you have to send the request to another service (or better a COM+ server application - see System.EnterpriseServices)
This service as to create a logon session for a user by calling LogonUser() (Win32 API) followed by a call to LoadUserProfile() (Win32 API) and ImpersonateLoggedOnUser() (Win32 API) before printing.
Note that calling LogonUser is not required if the appliaction is a COM+ application that runs with the credentials of an existing interactive user.



Willy.

Hello,

I am having a little trouble accessing a list of printers on our Network through a web service...

I am using the PrinterSettings.InstalledPrinters to access a list of printers installed on the local machine which works fine when using a test windows application on the server and calling the function directly.

However if the test application first calls a Web Service which then calls the function to retrieve a list of prints I only get a small sub-set of printers installed on the machine.

I believe this problem is related to permissions for the user under which the Web Service runs but I am not sure, does anyone have any ideas on this.

Regards,

Bill
 
B

Bill Gates

Thanks for the responses they have been very helpful, however still having a little problem as outlined below...

I have taken the code from http://www.codeproject.com/csharp/cpimpersonation1.asp which is used to impersonate a user and implemented this in my project however i am still unable to print through a web service.

The code that I am using to try to print a report is...


--------------------------------------------------------------------------------

private System.Security.Principal.WindowsImpersonationContext newUser;

newUser = ImpersonateUser("username", "domain", "password"); // where ImpersonateUser is taken from the CodeProject source

// New User

string sNewUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name; // Can use this to check the current user


// Run Print

PalletLabel pl = new PalletLabel(); // Crystal Report called PalletLabel.rpt

pl.PrintOptions.PrinterName = [PrinterName]; // PrinterName is the UNC path of the printer (ie \\MyServer\MyPrinter)

pl.PrintToPrinter(1,true,1,1); // Print the damn thing


--------------------------------------------------------------------------------


When I call the web service and pass a printer i get the following error message

Error in File C:\DOCUME~1\dotNET\ASPNET\LOCALS~1\Temp\temp_bd4f3ceb-6c6e-41b7-bbad-b938cfee7d62.rpt:
Request cancelled by the user.
the code fails at the line "pl.PrintToPrinter(1,true,1,1); // Print the damn thing"

Does anyone have any ideas about this???
If you want I could post a small app with my code in if you think that will help.

Regards

Bill

Hello,

I am having a little trouble accessing a list of printers on our Network through a web service...

I am using the PrinterSettings.InstalledPrinters to access a list of printers installed on the local machine which works fine when using a test windows application on the server and calling the function directly.

However if the test application first calls a Web Service which then calls the function to retrieve a list of prints I only get a small sub-set of printers installed on the machine.

I believe this problem is related to permissions for the user under which the Web Service runs but I am not sure, does anyone have any ideas on this.

Regards,

Bill
 
W

Willy Denoyette [MVP]

Thanks for the responses they have been very helpful, however still having a little problem as outlined below...

I have taken the code from http://www.codeproject.com/csharp/cpimpersonation1.asp which is used to impersonate a user and implemented this in my project however i am still unable to print through a web service.

The code that I am using to try to print a report is...


------------------------------------------------------------------------------

private System.Security.Principal.WindowsImpersonationContext newUser;

newUser = ImpersonateUser("username", "domain", "password"); // where ImpersonateUser is taken from the CodeProject source

// New User

string sNewUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name; // Can use this to check the current user


// Run Print

PalletLabel pl = new PalletLabel(); // Crystal Report called PalletLabel.rpt

pl.PrintOptions.PrinterName = [PrinterName]; // PrinterName is the UNC path of the printer (ie \\MyServer\MyPrinter)

pl.PrintToPrinter(1,true,1,1); // Print the damn thing


------------------------------------------------------------------------------


When I call the web service and pass a printer i get the following error message

Error in File C:\DOCUME~1\dotNET\ASPNET\LOCALS~1\Temp\temp_bd4f3ceb-6c6e-41b7-bbad-b938cfee7d62.rpt:
Request cancelled by the user.
the code fails at the line "pl.PrintToPrinter(1,true,1,1); // Print the damn thing"

Does anyone have any ideas about this???
If you want I could post a small app with my code in if you think that will help.


Now you only have created a new logon session, but a few questions remain:

- Was it successful? I mean how do you know it succeeded? What action is taken when it fails?
- Does the impersonating account has access to the remote printer share?
- What if the CR needs access to the HKCU of the impersonating user? (Note that it's hive is not loaded, you didn't load the users profile like I told you)
- What if there's a spooler/printer error message "displayed" when/before printing? Note that server side this message is not visible, and even if it was, who's gonna take the corrective action?


Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top