Fausto Carasai
Fausto Carasai

Reputation: 251

Trying to turn on/off iis website in c#

I looking for a c# .Net application (web or desktop) that stop/start a IIS website.

I was searching on internet about this and i can't find a example or explanation that works.

I found this dll http://msdn.microsoft.com/en-us/library/microsoft.web.administration.servermanager%28v=VS.90%29.aspx

this is an example that i'm doing to try to stop/start a IIS website:

"using Microsoft.Web.Administration" reference..

var server = new ServerManager();
var site = server.Sites.FirstOrDefault(s => s.Name == "SITENAME");  
if (site != null)
        {
            //stop the site...
            site.Stop();
            if (site.State == ObjectState.Stopped)
            {
                //do deployment tasks...
            }
            else
            {
                throw new InvalidOperationException("Could not stop website!");
            }
            //restart the site...
            site.Start();
        }
        else
        {
            throw new InvalidOperationException("Could not find website!");
        }

site.Stop throw an exception:

"Access denied. (Excepción de HRESULT: 0x80070005 (E_ACCESSDENIED))"

ExceptionType "UnauthorizedAccessException"

How can i do this? what im doing wrong?

Upvotes: 1

Views: 3648

Answers (2)

kingdango
kingdango

Reputation: 3999

Expanding on my comment: The error seems pretty obvious, the security context your application is running under does not have permission. If your program itself is running under IIS make sure the app pool it's using has a privileged user account.

In IIS your website runs under an Application Pool. Make sure that application pool is using an Identity that has adequate permission. The default ApplicationPoolIdentity account does not have elevated permissions for good reason.

For testing purposes you can use your own login for this since you indicated it had administrative permissions.

enter image description here

Upvotes: 3

Gabriel Graves
Gabriel Graves

Reputation: 1785

You will need to run the program as an administrator.

Upvotes: 1

Related Questions