我正在使用Microsoft.Web.Administration库来管理IIS内部的WebSite。我需要找到正确的WebSite,停止它,做一些事情,然后重新启动它。
只要我使用管理员帐户,就可以正常工作。但在实际场景中,我们需要使用本地帐户,该帐户负责与我们的软件相关的任务。该帐户是本地管理员,可以使用IIS管理器管理IIS。
如果我授予该帐户访问C:\Windows\system32\inetsrv\config的权限,则可以列出WebSites,但不能调用.Stop()。如果我这样做,我们会得到E_ACCESSDENIED错误。
那么,有没有办法授予本地帐户使用Microsoft.Web.Administration.ServerManager管理站点的权限,还是只允许真正的管理员帐户使用?
示例
ServerManager serverManager = new ServerManager();
serverManager.Sites["MyWebSite"].Stop();代码在C#应用程序内部运行
发布于 2018-04-10 03:32:38
我使用System.Diagnostics命名空间中的c#进程完成此操作,使用命令并以管理员身份运行它。这是我的函数,用于停止IIS应用程序池,您可以将其更改为停止网站:
void ManageIisApplicationPool(string scriptPath, string poolName, string action)
{
Process process = new Process();
string cmdPathStop = $"{scriptPath}{poolName}_{action}.cmd";
string stopCommand = "C:\\Windows\\System32\\inetsrv\\appcmd " +
$"{action} apppool /apppool.name:{poolName}";
if (!File.Exists(cmdPathStop))
{
File.Create(cmdPathStop).Dispose();
using (var tw = new StreamWriter(cmdPathStop))
{
tw.WriteLine(stopCommand);
tw.Close();
}
}
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = cmdPathStop,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = true,
Verb = "runas"
};
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
}实际上代码有一些变通的问题,它将cmd命令保存到.cmd文件中,然后运行它,如果你擅长cmd函数,你可以使用参数(将网站名称作为cmd函数参数传递)。唯一的问题是,当你运行带有"admin“动词的process时,你不会得到错误。希望这能有所帮助。
https://stackoverflow.com/questions/49667153
复制相似问题