[英]Stopping WebSite Microsoft.Web.Administration AccessDenied
我正在使用Microsoft.Web.Administration
庫來管理IIS中的WebSite。 我需要找到正確的WebSite,停止它,做一些事情然后重新啟動它。
只要我使用管理員帳戶,它就可以正常工作。 但是在實際情況下,我們需要使用本地帳戶,該帳戶可以響應有關我們軟件的任務。 該帳戶是本地管理員,可以使用IIS-Manager管理IIS。
如果我為帳戶授予對C:\\Windows\\system32\\inetsrv\\config
訪問權限,則可以列出網站,但不能調用.Stop()
。 如果這樣做,我們會收到E_ACCESSDENIED
錯誤。
因此,有沒有辦法授予本地帳戶使用Microsoft.Web.Administration.ServerManager
來管理站點的權限,還是只可能使用真正的Administrator-Account?
例
ServerManager serverManager = new ServerManager();
serverManager.Sites["MyWebSite"].Stop();
該代碼在C#-Application內部運行
我是使用System.Diagnostics命名空間中的c#進程,使用cmd並將其作為Admin運行的。 這是我的功能,它將停止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”動詞運行流程時,不會出錯。 希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.