Reputation: 433
Solved - thanks to Steve. The problem was that the App Pool I used was using the default App Pool Identity, which doesn't have sufficient privileges. Changing to an App Pool that used an account with sufficient privileges fixed the problem.
I have a simple C# class to get a list of available printers. When called from within a simple Console app, it returns the complete list of locally defined printers (9 printers). When called from the code behind in an aspx web app, it only returns the default printer. The web app is installed and runs as a user that is a local administrator on the server, and all printers are defined within that user's profile.
Note - I remote desktop to the server and run the console exe app there and it shows all the printers. I am trying to get a list of all the printers defined on the server.
Is there some security issue that I am missing?
Class code:
using System.Management;
/// <summary>
/// Returns a list of printers available on the current system.
/// </summary>
public static class PrinterList
{
public class Printer
{
public string name { get; set; }
public string server { get; set; }
public string location { get; set; }
public string portname { get; set; }
public string sharename { get; set; }
}
public static Printer[] GetInstalledPrinterList()
{
Printer[] printers = new Printer[0];
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * from Win32_Printer");
ManagementObjectCollection coll = searcher.Get();
foreach (ManagementObject printer in coll)
{
string name = printer.GetPropertyValue("Name").ToString();
if ((!name.ToLower().Contains("send to onenote")) &&
(!name.ToLower().Contains("xps document writer")) &&
(!name.ToLower().Contains("print to pdf")) &&
(!name.ToLower().Contains(" fax")) &&
(!name.ToLower().Equals("fax")))
{
string server = "";
if (printer.GetPropertyValue("ServerName") != null)
server = printer.GetPropertyValue("ServerName").ToString();
string location = "";
if (printer.GetPropertyValue("Location") != null)
location = printer.GetPropertyValue("Location").ToString();
string portname = "";
if (printer.GetPropertyValue("PortName") != null)
portname = printer.GetPropertyValue("PortName").ToString();
string sharename = "";
if (printer.GetPropertyValue("ShareName") != null)
sharename = printer.GetPropertyValue("ShareName").ToString();
Printer newPrinter = new Printer();
newPrinter.name = name;
newPrinter.server = server;
newPrinter.location = location;
newPrinter.portname = portname;
newPrinter.sharename = sharename;
Printer[] newlist = new Printer[printers.Length + 1];
for (int i = 0;i < printers.Length; i++)
{
newlist[i] = printers[i];
}
newlist[printers.Length] = newPrinter;
printers = newlist;
}
}
return printers;
}
}
Upvotes: 0
Views: 105
Reputation: 11963
Your asp.net c# code runs on your server not on client's computer. This is really important fact to remember.
By calling get printers on asp.net you are basically getting the available printers connected to your server not the ones from client.
Edit: Turns out that you were using the default account for the app pool. By using an account with admin right for the app pool solves the problem.
Upvotes: 1