appcmd.exe由于权限不足,无法读取配置文件

时间:2014-05-14 15:29:11

标签: c# asp.net iis-7.5 appcmd

我已按照Cannot read configuration file due to insufficient permissions

的步骤进行操作

我还尝试手动设置相关文件的权限,但这不起作用,尤其是在appcmd.exe上。

我正在尝试更新applicationHost.config以在我的网站加载IIS时动态设置IIS重置时间。为此,我试图在我的项目的global.asax文件中执行以下代码:

string WindowsDir = Server.MapPath("~/Startup");
string command = WindowsDir + @"\Startup.cmd";
string outputFilePath = WindowsDir + @"\log.txt";
string arguments = String.Format(
    "/c echo Startup task (Startup.cmd) executed at {0} >>\"{1}\"", 
    System.DateTime.UtcNow.ToString(),
    outputFilePath);
// System.Diagnostics.Process.Start(command, arguments);

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = command;
startInfo.Arguments = arguments;

process.StartInfo = startInfo;
process.Start();

当我在Visual Studio中运行我的网站时,这非常有效,但是当我在IIS上部署它时,会产生以下错误:

  

防止IIS应用程序池因空闲而关闭:

     

appcmd set config -section:applicationPools -applicationPoolDefaults.processModel.idleTimeout:00:00:00 /commit:apphost

ERROR ( message:Configuration error 
Filename: redirection.config
Line Number: 0
Description: Cannot read configuration file due to insufficient permissions. )
     

在UTC时间上午8:00(MST凌晨1:00)安排IIS应用程序池回收:

     

appcmd set config -section:applicationPools -applicationPoolDefaults.recycling.periodicRestart.schedule.[value='08:00:00']" /commit:apphost

ERROR ( message:Configuration error Filename: redirection.config
Line Number: 0
Description: Cannot read configuration file due to insufficient permissions. )
     

防止IIS应用程序池回收在1740分钟(29小时)的默认计划中回收:

     

appcmd set config -section:applicationPools -applicationPoolDefaults.recycling.periodicRestart.time:00:00:00 /commit:apphost

ERROR ( message:Configuration error 
Filename: redirection.config
Line Number: 0
Description: Cannot read configuration file due to insufficient permissions. )

然后我尝试执行以下代码:

string WindowsDir = Server.MapPath("~/Startup");
string command = WindowsDir + @"\Startup.cmd";
string outputFilePath = WindowsDir + @"\log.txt";
string arguments = String.Format(
    "/c echo Startup task (Startup.cmd) executed at {0} >>\"{1}\"", 
    System.DateTime.UtcNow.ToString(),
    outputFilePath);
// System.Diagnostics.Process.Start(command, arguments);

var process = new System.Diagnostics.Process();
var startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = command;
startInfo.Arguments = arguments;

string name = "Administrator";
string pass = "password";

startInfo.Password = pass.Aggregate(new System.Security.SecureString(), (ss, c) => { ss.AppendChar(c); return ss; });
startInfo.UserName = name;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;

process.StartInfo = startInfo;
process.Start();

然后它甚至从Visual Studio也无法工作。然后我又回到了在Visual Studio中工作的代码(如上所述)。并在Web.config<system.web>标记内)中添加了以下标记:

<authentication mode="Windows"/>
<identity impersonate="true" userName="Administrator" password="password"/>

但是当我在IIS(7.5)上运行网站时,这并没有起作用。任何想法如何使这项工作?

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题(我使用的是IIS 8,Windows 8,C#,VS 2013),有时代码你没有权限。我正在执行:

string windir = Environment.GetEnvironmentVariable("windir");    
string comando = windir +"\\System32\\inetsrv\\appcmd.exe set site /site.name:test /+bindings.[protocol='http',bindingInformation='*:80:mitest']";

System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + comando);

我收到错误:&#34;由于权限不足而无法读取配置文件&#34;

最后使用 ServerManager 类,首先我在项目中引用此dll:

<强> C:\ Windows \ System32下\ INETSRV \ Microsoft.Web.Administration.dll

然后我使用代码来操作AppPools,Sites或Bindings:

using (ServerManager serverManager = new ServerManager())
{
    if (serverManager.ApplicationPools == null)
        return;

    for (int i = 0; i < serverManager.Sites.Count; i++)
    {
        Microsoft.Web.Administration.ApplicationPoolCollection appPoolCollection = serverManager.ApplicationPools[i];
    }

    if (serverManager.Sites == null)
        return;

    for (int i = 0; i < serverManager.Sites.Count; i++)
    { 
       Microsoft.Web.Administration.BindingCollection bindingCollection = serverManager.Sites[i].Bindings;
    }
}