views:

749

answers:

3

Hello,

I need to run one console application from ASP.NET application using Administrator account and with Desktop interaction enabled. I have tried code below, console app runs ok but within NETWORK SERVICE account. Any ideas how to run console under Administrator account?

    string enginePath = Server.MapPath(@"~/engine/MyConsole.exe");
    System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(enginePath, "");
    System.Diagnostics.Process p = System.Diagnostics.Process.Start(info);            
    p.WaitForExit();

Regards, Tomas

A: 

you could use impersonation, there is an example here

personally i dont like impersonation in asp.net, you need to deal with passwords either not being changed or changing them in code. Is there no way to run what you want as the asp.net user?

edit:

You could acyually impersonate the network service by using "NETWORK SERVICE" as the user name, that would at least allieviate the password issues a little,

Pharabus
Unfortunately, ASP.NET account do not have impersonation privileges, so I can't login to Administrator account from ASP.NET. Seems closed cycle.
Tomas
A: 

You can use a manifest file and built it into your console application that will instruct it to always run under an admin account. See this example.

If this doesn't work for you then you could try passing in Admin account credentials in the ProcessStartInfo property e.g.

string enginePath = Server.MapPath(@"~/engine/MyConsole.exe");
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(enginePath, "");
info.UserName = "Administrator";
info.Password = "Password";
System.Diagnostics.Process p = System.Diagnostics.Process.Start(info);                p.WaitForExit();
James
Not sure how embedding manifest file can obtain Administrator rights for console which was run from NETWORK SERVICE account(ASP.NET).I have embedded manifest file to my console application but as expected it runs only under asp.net account.<?xml version="1.0" encoding="utf-8"?><assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"> <security> <requestedPrivileges> <requestedExecutionLevel level="requireAdministrator" /> </requestedPrivileges> </security> </trustInfo></assembly>
Tomas
See http://msdn.microsoft.com/en-us/library/bb756929.aspx for more details on what the actual parameters mean. But by the looks of it it should run with the Admin account if you use "requireAdministrator".
James
A: 

Another user already suggested impersonation. If that's good enough, there you go. Like he said, though, there are some maintenance headaches to deal with and some security implications.

Some options that I've used in the past which may or may not be applicable in your situation are:

  1. If the task is on a predictable schedule, just add it to the Scheduled Tasks in Windows, set the appropriate worker account (Administrator, or whatever), and let 'er go. I believe there are also ways to programmatically trigger a scheduled task, but I've never had to do that. A Google search should get you going.

  2. Implement the console app logic as a service running under the appropriate account. Then have the service listen for a "trigger" from your web app--a file drop or something simpler.

Either way the idea is to avoid storing any credientials in your ASP page, and to not have to grant that process rights it doesn't need.

Dave