简体   繁体   English

如何从asp.net Web服务在特定帐户下运行外部exe?

[英]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. 波纹管是我从asp.net服务尝试运行某些外部exe的代码。 It works fine from my Visual Studio on win 7, but fails on my server (server 2008). 在win 7上,我的Visual Studio正常运行,但在我的服务器(服务器2008)上失败。 Myapp.exe reports back eror that account under which is runned doesn't have sufficiet priviliges. Myapp.exe报告错误,表示该帐户运行所在的帐户没有足够的特权。

    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. 有人告诉我,运行asp.net辅助线程的帐户施加了一些其他限制。 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. 我还发现了一些关于使用pInvoke和win32 api调用作为从asp.net服务运行外部代码的唯一方法。 But I don't have any win32 api knowlege nor did I found exples of this. 但是我没有任何win32 api知识,也没有发现这些东西。

I would be very grateful for any tip/example how to run external exe under specified account from asp.net service. 我将不胜感激任何技巧/示例,如何在asp.net服务的指定帐户下运行外部exe。

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 WindowsIdentity.Impersonate方法

Or configure IIS to run the application under a user account with the required privileges. 或将IIS配置为以具有所需特权的用户帐户运行该应用程序。

Here is an article which explains different methods of impersonation security: 这是一篇文章,介绍了模拟安全性的不同方法:

Understanding ASP.NET Impersonation Security 了解ASP.NET模拟安全性

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 用于模拟用户的小型C#类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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