Пример #1
0
        /// <summary>
        /// Set specific account anonymous authentication for an IIS application
        ///
        /// The default behaviour for IIS is to have the ANONYMOUS user (that is
        /// used for all request) identified as IUSR. When using FAST-CGI impersonation,
        /// we WANT all permissions to be based on the application pool identity...
        /// </summary>
        /// <param name="siteName"></param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        public static void ConfigureAnonymousAuthForIisApplication(
            string siteName,
            string username,
            string password)
        {
            using (ServerManager serverManager = new ServerManager())
            {
                // fastCgi settings in IIS can only be set at the HOSTS level
                // we found no way to set this at a web.config level.
                Configuration        config = serverManager.GetApplicationHostConfiguration();
                ConfigurationSection section;

                // TODO: The type of authentication and it's configuration should be configurable here...
                // see https://www.iis.net/configreference/system.webserver/security/authentication
                section = config.GetSection("system.webServer/security/authentication/anonymousAuthentication", siteName);

                section["enabled"]  = true;
                section["password"] = password;
                section["username"] = username;

                UtilsIis.CommitChanges(serverManager);
            }
        }
Пример #2
0
        /// <summary>
        /// Changes the state of a site (start/stop/reset) including it's application pools.
        ///
        /// The changes are made persistent through the ServerAutoStart option.
        /// </summary>
        /// <param name="sitename"></param>
        /// <param name="action"></param>
        /// <param name="skipApplicationPools"></param>
        /// <returns></returns>
        public void WebsiteAction(
            string sitename,
            AppPoolActionType action,
            bool skipApplicationPools = false)
        {
            using (ServerManager manager = new ServerManager())
            {
                // Buscamos el site....
                var site = UtilsIis.FindSiteWithName(manager, sitename, this.Logger).SingleOrDefault();

                if (site == null)
                {
                    return;
                }

                // Cargamos TODOS los application pools de ese site...
                List <ApplicationPool> pools = new List <ApplicationPool>();

                if (!skipApplicationPools)
                {
                    foreach (var s in site.Applications)
                    {
                        var applicationPool =
                            manager.ApplicationPools.SingleOrDefault(i => i.Name == s.ApplicationPoolName);

                        if (applicationPool == null)
                        {
                            throw new Exception(
                                      string.Format(
                                          "Could not find application pool with name '{3}' for application with path '{0}' and site '{1}' ({2}).",
                                          s.Path,
                                          site.Name,
                                          site.Id,
                                          s.ApplicationPoolName));
                        }

                        pools.Add(applicationPool);
                    }
                }

                switch (action)
                {
                case AppPoolActionType.Start:
                    // Start all pools, then the site
                    foreach (var p in pools)
                    {
                        this.StartAppPool(p);
                    }

                    this.StartSite(site);
                    site.ServerAutoStart = true;
                    break;

                case AppPoolActionType.Stop:
                    // Stop site, then pools
                    this.StopSite(site, 10000);
                    foreach (var p in pools)
                    {
                        this.StopAppPool(p, 10000);
                    }

                    site.ServerAutoStart = false;
                    break;

                case AppPoolActionType.Reset:
                    // Stop site
                    this.StopSite(site, 10000);

                    // Stop pools
                    foreach (var p in pools)
                    {
                        this.StopAppPool(p, 10000);
                    }

                    // Start pools
                    foreach (var p in pools)
                    {
                        this.StartAppPool(p);
                    }

                    // Start site
                    this.StartSite(site);
                    break;
                }

                // Commit because we changed the autostart property
                UtilsIis.CommitChanges(manager);
            }
        }