简体   繁体   中英

Stopping WebSite Microsoft.Web.Administration AccessDenied

Im using the Microsoft.Web.Administration libary to manage a WebSite inside IIS. I need to find the correct WebSite, stop it, do stuff and start it again.

This works fine, as long as I use the Administrator account. But in the real scenario, we need to use a local account, which is reponsive for tasks regarding our software. The account is a local administrator and can manage the IIS with the IIS-Manager.

If I give the account Access to C:\\Windows\\system32\\inetsrv\\config , it's possible to list the WebSites, but not to call .Stop() . If I do so, we get E_ACCESSDENIED error.

So is there way to give the local account permissions to manage Sites using Microsoft.Web.Administration.ServerManager or is it just possible for the real Administrator-Account?

Example

ServerManager serverManager = new ServerManager();
serverManager.Sites["MyWebSite"].Stop();

The code is running inside a C#-Application

I did this using c# processes from System.Diagnostics namespace, using cmd and running it as Admin. This is my function, which stops IIS Application pool, you can change it to stop website:

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();
    }

Actually the code has some workaround issue, it saves the cmd command into .cmd file and then runs it, if you are good at cmd functions you can do this with arguments(pass website name as a cmd function argument). The only problem is that when you run process with "admin" verb, you can't get errors. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM