简体   繁体   English

启动过程不适用于模拟

[英]Start Process not working with Impersonation

I am having trouble invoking a batch file with impersonation (.NET 4.0) 我无法通过模拟调用批处理文件(.NET 4.0)

I have a web application that invokes a test batch file residing on the local file system of the server. 我有一个Web应用程序,该应用程序调用驻留在服务器本地文件系统上的测试批处理文件。 When I run it without impersonation it runs fine. 当我不假冒地运行它时,它运行良好。 But with impersonation the batch file doesnt produce any output, nor does it returns any error. 但是通过模拟,批处理文件不会产生任何输出,也不会返回任何错误。

Here is the code for executing the batch file that i use - 这是执行我使用的批处理文件的代码-

public static bool ExecuteBatchFile(string fileLocationPath, string filePath, string arguments, TextBox textBox)
        {
            try
            {
                ProcessStartInfo procStartInfo = new ProcessStartInfo(filePath);
                procStartInfo.UseShellExecute = false;
                procStartInfo.Arguments = arguments;
                procStartInfo.WorkingDirectory = fileLocationPath;
                procStartInfo.RedirectStandardOutput = true;
                procStartInfo.RedirectStandardError = true;
                procStartInfo.RedirectStandardInput = true;
                procStartInfo.UserName = "XYZ";
                procStartInfo.Domain = "ABC";
                System.Security.SecureString pwd = new System.Security.SecureString();
                foreach (char c in "PWD")
                    pwd.AppendChar(c);
                procStartInfo.Password = pwd;
                procStartInfo.LoadUserProfile = true;

                Process proc = new Process();
                proc.StartInfo = procStartInfo;
                proc.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e)
                {
                    textBox.Text += "output rcvd\r\n";
                    textBox.Text += e.Data;
                };
                proc.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e)
                {
                    textBox.Text += "error rcvd\r\n";
                    textBox.Text += e.Data;
                };
                proc.Start();
                proc.BeginOutputReadLine();
                proc.BeginErrorReadLine();
                proc.WaitForExit();
                proc.Close();
                return true;
            }
            catch (Exception e)
            {
                textBox.Text += e.Message;
                return false;
            }
        }

Without impersonation i can see the output of the batch file. 没有模拟,我可以看到批处理文件的输出。 I get Output received with output of batch file and then an empty OutputReceived, and then an empty ErrorReceived. 我得到的输出与批处理文件的输出一起接收,然后为空的OutputReceived,然后为空的ErrorReceived。

But with impersonation, i can see nothing! 但是冒充,我什么也看不见! Just one event for OutputReceived with no data, and one event for ErrorReceived with no data. 对于没有数据的OutputReceived只有一个事件,对于没有数据的ErrorReceived只有一个事件。

I have set impersonation in the web config file as follows: 我已在网络配置文件中设置模拟,如下所示:

<identity impersonate="true" userName="ABC\XYZ" password="PWD"/>

I experienced similar problem with Windows 2003 Server where my service hosted in IIS was executing application. 我在Windows 2003 Server上遇到了类似的问题,其中在IIS中托管的服务正在执行应用程序。

Things i discovered: 1. You must ajust permissions on service and configure it to output stack trace (including folder where .Net puts built service). 我发现的事情:1.您必须调整对服务的权限并将其配置为输出堆栈跟踪(包括.Net放置已建立服务的文件夹)。 2. Starting app with loading of user profile cannot be done from service. 2.无法通过服务完成加载用户配置文件的启动应用程序。 You can create wrapper application which you will execute without loading profile, and that app can in its turn execute app with loading of user profile. 您可以创建将在不加载配置文件的情况下执行的包装器应用程序,而该应用程序又可以在加载用户配置文件的情况下执行该应用程序。 Wrapper must be built in Release mode and mustn't call Trace functions. 包装器必须以发布模式构建,并且不得调用跟踪功能。 3. On service change you must - restart application pool in IIS snapp-in - restart site - execute iisreset. 3.在更改服务时,您必须-重新启动IIS管理单元中的应用程序池-重新启动站点-执行iisreset。

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

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