简体   繁体   中英

How to run external exe under specific account from asp.net web service?

Bellow is my code from asp.net service which is trying to run some external exe. It works fine from my Visual Studio on win 7, but fails on my server (server 2008). Myapp.exe reports back eror that account under which is runned doesn't have sufficiet priviliges.

    List<ProcInfo> allProcesses = new List<ProcInfo>();            
    ProcessStartInfo pInfo = new ProcessStartInfo();
    pInfo.FileName = binPath + @"\myApp.exe";
    pInfo.WindowStyle = ProcessWindowStyle.Hidden;
    pInfo.CreateNoWindow = true;
    pInfo.UseShellExecute = false;
    pInfo.RedirectStandardOutput = true;

    string exitMsg = "";
    int exitCode = 1;
    try
    {
        using (Process proc = Process.Start(pInfo))
        {
            exitMsg = proc.StandardOutput.ReadToEnd();
            proc.WaitForExit(1000);
            exitCode = proc.ExitCode;
        }
    }

Resource pool on the server runs under account with sufficient priviliges and I also tried to use same account in code to start service with those same credentials and still nothing.

I have been told that account under which asp.net worker thread runs impose some additional limitations. So even if resource pool runs under appropriate account, you still won't have sufficient priviligies. I also found something about using pInvoke and win32 api calls as the only way to run external code from asp.net service. But I don't have any win32 api knowlege nor did I found exples of this.

I would be very grateful for any tip/example how to run external exe under specified account from asp.net service.

If the account the worker process is runnning under lacks sufficient privelages then you have a few options.

You can either use impersonation in your code:

WindowsIdentity.Impersonate Method

Or configure IIS to run the application under a user account with the required privileges.

Here is an article which explains different methods of impersonation security:

Understanding ASP.NET Impersonation Security

If you do not feel confident implementing the user impersonation code yourself, here is a link to a codeproject article:

A small C# Class for impersonating a User

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