I've got an aps.net site. Can I start a print request for a pdf from a client to a printer connected only to the server?
In my server I use this code in a web api
[HttpPost]
public void Print()
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = #"C:\Users\Me\Documents\Doc.pdf";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.StartInfo = info;
p.Start();
}
If I run this request while debugging it works (i mean it prints the doc correctly), but if I start the request with with postman from a virtual machine (to simulate a network request) and debug the process attached to a IIS site no error will rise but no print done. The debug steps correctly all the instructions.
I set my application pool with my user identity (admin) and "connect as" on the site as well as my user.
I don't know if It can't be done cause on ASP.NET or cause I'm missing something.
This will do. I print with foxit reader. Maybe my default, Acrobat, have some isseus or asking for user interaction.
Related
I have the below command which run batch file, I need to run this batch when I open the ASPX page in the machine browser to affect the machine and not affect server :
ProcessStartInfo psi = new ProcessStartInfo(this.WhiteLabel.Text);
psi.RedirectStandardOutput = false;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = true;
Process.Start(psi);
This is not possible because of security concerns. The only possible way might be to create an ActiveX library that the user would acknowledge and accept on their browser to run. What are you trying to run on the client? Maybe there is another approach?
This has been asked before see here...
How to execute an application on the client from a website?
I am trying to run a jar in C# by running this code:
System.Diagnostics.ProcessStartInfo processInfo = new System.Diagnostics.ProcessStartInfo(pathForjre+"java.exe", "-jar "+jar+" "+argsforjar);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = processInfo;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = false;
string out = "";
proc.Start();
proc.WaitForExit();
while (!proc.StandardOutput.EndOfStream)
out += proc.StandardOutput.ReadLine();
proc.Close();
return out;
When the jar runs I get an access denied exception with the path the jar is trying to write the log file to. I can manually run the jar from the command line and the log file writes no problem. How do I give the process permission to do things like write a file?
ASP.NET apps by default don't impersonate the identity of the user that is hitting the site. Typically then run as the local Network Service account unless you specifically have the process run under a different identity (you can determine the identity it's using by looking at Environment.UserName).
You can either set up the site (or use impersonation) to run under an identity that has permission to write to that location, or give write permision to the account that it's currently using.
Setting the working directory of the process resolved the issue.
proc.StartInfo.WorkingDirectory = path;
I have the below code in ASP.NET C# and it work fine in local system + production machine when i tested in debug mode. but it doesn't work when i uploading to IIS.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = Server.MapPath(filePath);
startInfo.Verb = "print";
startInfo.Arguments = "Printer Name";
Process proc = new Process();
proc.StartInfo = startInfo;
proc.Start();
proc.WaitForExit(5000);
if (proc.HasExited == false)
{
proc.Kill();
}
Things i tried.
Control panel > Admin Services > Services > IIS Admin Service > Log on Tab > check to interact with desktop. Reset IIS Admin and IIS.
Printer Properties > Security > Grand ASPNET, NETWORK SERVICE, EVERYONE to full access.
Tried to set another printer as Default Printer. Reinstall / Add Printer.
I tried all the above with no success. finally i tried below in my machine.config.
WINNT>Microsoft.NET>Framework>v2.52something>Config> machine.config
I replaced this
processModel autoConfig="true"
with this
processModel userName="SYSTEM" password="AutoGenerate"
and i am getting this message
"Before you can perform print-related tasks you need to install a
printer"
i am using acrobat 7 and i can print the test page from printer itself and from acrobat software.
you can use Verb if acrobat is installed on you machine. and pass printer name as arguments
var fileName = #"c:\pdf\file.pdf";
var startInfo = new ProcessStartInfo(fileName);
string verbToUse = "PrintTo";
startInfo.Verb = verbToUse;
startInfo.Arguments = "PrinterName";
Process p = Process.Start(startInfo);
The issue may be that IIS runs under a different user that has less permissions than a typical User. See System.Diagnostics.Process.Start not work from an IIS
I fixed it on my server by changing the ProcessModel Identity to a user that has permissions. Probably a workaround and bad practice, but it worked. {Application Pool} -> Advanced Settings -> Identity -> Custom Account (Also toggle Load User Profile to true)
I am using the following code to run a batch file from C#. The following code is part of my windows service. This code works perfectly fine in Windows XP but when I deploy my windows service to Windows server 2003 OS it returns exit code 1 (failure). Does someone know what I am missing? Do I need to give some special permission to the windows service? The service is installed as a Local System service.
ProcessStartInfo psi = new ProcessStartInfo();
//specify the name and arguments to pass to the command prompt
psi.FileName = ConfigurationManager.AppSettings["BatchFilePath"];
psi.Arguments = fileName;
//Create new process and set the starting information
Process p = new Process();
p.StartInfo = psi;
//Set this so that you can tell when the process has completed
p.EnableRaisingEvents = true;
p.Start();
//wait until the process has completed
while (!p.HasExited)
{
System.Threading.Thread.Sleep(1000);
}
//check to see what the exit code was
if (p.ExitCode != 0)
{
logger.Write("Exit Code" + p.ExitCode);
}
My next set would be to try setting the service to run as the user you're logged in as when it works. That way you'll know whether it is something specific to the Network Service account that's stopping it working
This following code is pretty simple and it works in a Console Application. But for some reason it does not work in a WCF Service. The directory which has the batch file has full permissions. Can someone help me? What am I missing?
try
{
ProcessStartInfo psi = new ProcessStartInfo();
//specify the name and the arguements you want to pass
psi.FileName = ConfigurationManager.AppSettings["BatchFileLocation"];
psi.Arguments = filePath;
//Create new process and set the starting information
Process p = new Process();
p.StartInfo = psi;
//Set this so that you can tell when the process has completed
p.EnableRaisingEvents = true;
p.Start();
//wait until the process has completed
while (!p.HasExited)
{
System.Threading.Thread.Sleep(1000);
}
//check to see what the exit code was
if (p.ExitCode != 0)
{
logger.Write(p.ExitCode);
}
}
catch (Exception ex)
{
logger.Write(ex.Message);
}
I assume this an IIS Hosted WCF Service.
Check the Identity associated with your application pool.
In IIS 7, and 6.0 to the best of my knowledge, this is the NetworkService, which may or may not have rights to the Batch file, SFTP, etc. In 7.5 there is another account: Default Application Pool ID Changed in IIS 7.5. It is also possible that your pool is using some other account, including a machine specific one, and not a domain account.