2

I have followed the steps at Cannot read configuration file due to insufficient permissions

I also tried to manually set the rights to related files, but that didn't work, especially on appcmd.exe.

I am trying to update applicationHost.config to set IIS reset time dynamically, when my website loads on IIS. For this I am trying to execute the following code in global.asax file of my project:

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();

This works perfectly when I run my website in Visual Studio, but when I deploy it on IIS, it produces the following errors:

Prevent the IIS app pools from shutting down due to being idle:

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. )

Schedule IIS app pool recycles for 8:00 AM UTC time (1:00 AM MST):

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. )

Prevent IIS app pool recycles from recycling on the default schedule of 1740 minutes (29 hours):

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. )

Then I tried to execute following code:

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();

Then it didn't work even from Visual Studio. I then reverted back to code that was working in Visual Studio (as mentioned above). And added the following tags in Web.config (inside <system.web> tag):

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

But that didn't work when I ran the website on IIS (7.5). Any ideas how to make this work?

4

1 回答 1

1

我有同样的问题(我使用的是 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);

我得到错误:“由于权限不足,无法读取配置文件”

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

C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll

然后我使用代码来操作 AppPools、站点或绑定:

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;
    }
}
于 2014-05-29T17:09:52.723 回答