简体   繁体   English

从C#权限问题执行批处理文件

[英]Executing batch file from C# Permission issue

I have a batch file to execute a VB script. 我有一个批处理文件来执行VB脚本。 While executing the batch file by double clicking will work, But when I have done the same with C# its working on my local environment but not in the staging server (windows server 2008r2), Is there any permission level i need to apply for this execution. 虽然通过双击执行批处理文件将起作用,但是当我使用C#进行同样的操作时,它可以在我的本地环境中工作,而不是在登台服务器(Windows Server 2008r2)中工作,是否需要申请此执行的任何权限级别。 From the staging server I can double click and execute the batch file... 从登台服务器,我可以双击并执行批处理文件...

I have logged in to the server with Administrator account and browsed the application as localhost. 我已经使用管理员帐户登录到服务器,并以localhost身份浏览了该应用程序。

Is there anything I'm missing on the execution of batch file from C#, 从C#执行批处理文件时,我缺少什么吗?

I don't think there is any problem with my C# code as its working fine on my local environment, anyway following is my C# code, 我认为我的C#代码没有任何问题,因为它可以在本地环境下正常工作,无论如何,以下是我的C#代码,

if (File.Exists(FileName*))
            {
                System.Diagnostics.ProcessStartInfo p = new System.Diagnostics.ProcessStartInfo(FileName);
                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                proc.StartInfo.FileName = FileName;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.UseShellExecute = false;

                proc.Start();

                proc.WaitForExit();

            }
            else
            {
                lblMsg.Text = "Sorry unable to process you request";
            }

* FileName is the path to batch file. * FileName是批处理文件的路径。 Also I have set full permission to the folders that containg both batch file and vbs files. 我也对包含批处理文件和vbs文件的文件夹设置了完全权限。

For this to work your app pool needs to be run as a user who has access to the batch file. 为此,您的应用程序池需要以有权访问批处理文件的用户身份运行。 Check how to change your app pool identity for IIS 7 or IIS 6 . 检查如何更改IIS 7IIS 6的应用程序池标识。

To expand on what Kartheek said: 进一步介绍一下Kartheek所说的话:

  • In IIS 7 application pools run as a app pool account, IISAPPPOOL\\AppPoolName 在IIS 7应用程序池中以应用程序池帐户运行IISAPPPOOL\\AppPoolName
  • In IIS 6 application pools run as Network Service 在IIS 6中,应用程序池作为Network Service运行
  • In either case, these accounts don't have any access a user's documents folder and (by default) can only read from common data stores. 无论哪种情况,这些帐户都无法访问用户的documents文件夹,并且(默认情况下)只能从通用数据存储中读取。

Generally you want to keep the app pool account because it helps segregate the data -- so what I would do is just make sure you grant read+execute permissions on the bat file you need for the app pool account. 通常,您要保留应用程序池帐户,因为它有助于隔离数据-因此,我要做的就是确保您为应用程序池帐户所需的bat文件授予了读取+执行权限。 You'll also need proper permissions on any filles/folders the bat needs to read/write from. 您还需要对蝙蝠需要读取/写入的所有填充物/文件夹具有适当的权限。

You do not need to change anything in your app to correct this problem, unless you want to IIS app to masquerade around as the user who is actually sitting at the website (it only really works if you use some form of authentication.) Generally this a bad idea anyway -- so it's best to just adjust the permissions. 您无需更改应用程序中的任何内容即可解决此问题,除非您希望IIS应用程序伪装成实际坐在网站上的用户(仅在使用某种形式的身份验证时才有效)。无论如何,这是一个坏主意-因此最好只是调整权限。

As a general rule of thumb, when working on a web server you want to keep the permissions/execution levels as low/restrictive as possible. 根据一般经验,在Web服务器上工作时,您希望将权限/执行级别保持为尽可能低/严格。

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

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