Reputation: 1750
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?
Upvotes: 2
Views: 4699
Reputation: 2047
I had the same problem (I was using IIS 8, Windows 8, C#, VS 2013), by code sometimes you have'not permissions. I was executing:
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);
And I get the Error: "Cannot read configuration file due to insufficient permissions"
At the end use the ServerManager class, first I reference this dll in my project:
C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll
then I use the code to manipulate the AppPools, Sites or 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;
}
}
Upvotes: 1