asp.net run program with Administrator account

送分小仙女□ 提交于 2019-12-07 06:02:31

问题


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


回答1:


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,




回答2:


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.




回答3:


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(); 


来源:https://stackoverflow.com/questions/1873796/asp-net-run-program-with-administrator-account

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!