简体   繁体   中英

ASP.Net run exe on server from an API call

I have a WebAPI that works great but need to add the ability to call to an EXE on the server to run some tasks with Video, the server is our machine and running IIS to host the WebAPI.

I have tried it working with Process() and the calls make it to the cmd.exe file I have written but the issue is that the user is IUSER and this won't work as the Video processing needs to use system hardware so needs to be the current logged in Windows User.

I don't want to give the IUSER this privilege for obvious security reasons so I am looking for another way to call and pass data to the an EXE or background task (Short running <3seconds) and for that process to reply with the results.

It's all on the server which we have full control over.

Current Code:

string exeLoc = Environment.ExpandEnvironmentVariables(baseEXELocation);
using var process = new Process();
process.StartInfo.FileName = exeLoc; 
process.StartInfo.Arguments = $"{command}";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += (sender, data) =>
{
   if (!String.IsNullOrEmpty(data.Data))
   {
      Console.WriteLine(data.Data);
      consoleData += data.Data;
    }
};
process.Start();
process.BeginOutputReadLine();
var exited = process.WaitForExit(1000 * 10);     // (optional) wait up to 10 seconds

Thanks

You could try to use the impersonation in iis:

<identity impersonate="true" />

https://support.microsoft.com/en-us/help/306158/how-to-implement-impersonation-in-an-asp-net-application

or assign the administrator permission to the application pool by using application pool advance setting -> identity to the custom account.

or You can try using the verb runas in Process.Start to execute the exe file as an Administrator.

ProcessStartInfo proc = new ProcessStartInfo();
proc.WindowStyle = ProcessWindowStyle.Normal;
proc.FileName = myExePath;
proc.CreateNoWindow = false;
proc.UseShellExecute = false;
proc.Verb = "runas"; //this is how you pass this verb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM