By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,359 Members | 1,543 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,359 IT Pros & Developers. It's quick & easy.

Network Printers and Web Services

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Bill Gates" <BG****@microsoft.com> wrote in message
news:eK*************@TK2MSFTNGP12.phx.gbl...
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
Nov 16 '05 #2

P: n/a
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.

"Bill Gates" <BG****@microsoft.com> wrote in message news:eK*************@TK2MSFTNGP12.phx.gbl...
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
Nov 16 '05 #3

P: n/a
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.WindowsImpersonationCont ext newUser;

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

// New User

string sNewUser = System.Security.Principal.WindowsIdentity.GetCurre nt().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_bd4f3 ceb-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

"Bill Gates" <BG****@microsoft.com> wrote in message news:eK*************@TK2MSFTNGP12.phx.gbl...
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
Nov 16 '05 #4

P: n/a
"Bill Gates" <BG****@microsoft.com> wrote in message news:uq**************@TK2MSFTNGP14.phx.gbl...
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.WindowsImpersonationCont ext newUser;

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

// New User

string sNewUser = System.Security.Principal.WindowsIdentity.GetCurre nt().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_bd4f3 ceb-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.

Nov 16 '05 #5

P: 1
Please note that the main problem is security, the application runs as ASPNET account on the OS, and that resticts it from process other things properly.
you can easily change that by impersonating , there are various options for that, i used the web config like that
<configuration>
<system.web>
<identity impersonate="true" userName="user" password="password" />


the user name of course , has to have permissions for that printer,
you can also do it by pasting this into the machine.config, but that will affect all your web applications.

that or search for code for dynamically doing that.
worked for me, probobly will work for you
Apr 26 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.