apppcmd .exe无法读取配置文件,因为权限不足
本文关键字:因为 权限 配置文件 exe 读取 apppcmd | 更新日期: 2023-09-27 18:03:16
我已经按照无法读取配置文件的步骤,由于权限不足
我还尝试手动设置相关文件的权限,但这不起作用,特别是在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)上运行网站时,这不起作用。有什么好主意吗?
我有同样的问题(我使用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
然后我使用代码来操作apppool, 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;
}
}