appcmd.exe 权限不足,无法读取配置文件
Posted
技术标签:
【中文标题】appcmd.exe 权限不足,无法读取配置文件【英文标题】:appcmd.exe Cannot read configuration file due to insufficient permissions 【发布时间】:2014-07-02 18:23:09 【问题描述】:我已按照Cannot read configuration file due to insufficient permissions的步骤进行操作
我也尝试手动设置相关文件的权限,但没有奏效,尤其是在appcmd.exe
。
当我的网站在 IIS 上加载时,我正在尝试更新 applicationHost.config
以动态设置 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. )
安排 IIS 应用程序池在 UTC 时间上午 8:00(MST 上午 1:00)回收:
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】:我遇到了同样的问题(我使用的是 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;
【讨论】:
以上是关于appcmd.exe 权限不足,无法读取配置文件的主要内容,如果未能解决你的问题,请参考以下文章
IIS------Http错误:50019,由于权限不足无法读取配置文件
win7 web开发遇到的问题-由于权限不足而无法读取配置文件,无法访问请求的页面