Permissions for app via a web API

  • Thread starter Thread starter lsavidge
  • Start date Start date
L

lsavidge

Guest
Hi,


I have a web API written in C# ASP.NET core 2.1. It runs through IIS and has a number of end points for client connections to pass data into, to create records in their Sage Accpac accounting system. The end points all accept XML documents in the request header. One of the end points allows for the client to pass in cheque (check for you in the US) payment records into Sage. The way this works is that the XML document they pass in is deserialised into an object and I pull out the relevant data I need and construct a call into Sage and post the record. That bit works just fine. There are limitations to the way the web API works and to get around them, I have written two external applications that run by being passed command line parameters, and the web API executes the application which returns statuses via its return code. The first application that is called will cause the cheque to be printed out on a given printer. If I run the application in standalone mode on a record that is in Sage, it will print the cheque on any printer I tell it to. The printer name and port are passed in as command line parameters. If I let the web API make the call to run the printing program, it does not select the printer correctly. This appears to be a quirk in the way Sage deals with selecting printers, so to get around this I added code that changes the default printer to the chosen one each time, which is fine, and again works when I run the program manually. It doesn't however, change the default printer when the application is called from the web API. I know the command line parameters it is passed are correct, as the web API logs the arguments out to a database. The cheque will print out, but not on the chosen printer because the changing of the default printer hasn't happened when it is called from the web API. This has a feel of permissions about it.


The web API runs in an app pool under a given user name in Windows. The cheque printing application is set to run as administrator, but I think that when it is running after being called from the web API that it picks its permissions up from the calling API rather than what has been set in Windows. Any ideas? I created a class to allow me to set the default printer.


public static class WindowsPrinters
{
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetDefaultPrinter(string Name);
}

Once the parameters have been picked up, I then make a call to that function to set it like this:

private void SetDefaultPrinterForSage(string psPrinterName)
{
WriteLog("Setting default printer to: " + psPrinterName);
WindowsPrinters.WindowsPrinters.SetDefaultPrinter(psPrinterName);
}

Continue reading...
 
Back
Top