/// <summary> /// Creates a IIS site /// </summary> /// <param name="settings">The setting of the site</param> /// <param name="exists">Check if the site exists</param> /// <returns>IIS Site.</returns> protected Site CreateSite(SiteSettings settings, out bool exists) { if (settings == null) { throw new ArgumentNullException("settings"); } if (string.IsNullOrWhiteSpace(settings.Name)) { throw new ArgumentException("Site name cannot be null!"); } //Get Site Site site = _Server.Sites.FirstOrDefault(p => p.Name == settings.Name); if (site != null) { _Log.Information("Site '{0}' already exists.", settings.Name); if (settings.Overwrite) { _Log.Information("Site '{0}' will be overriden by request.", settings.Name); this.Delete(settings.Name); ApplicationPoolManager .Using(_Environment, _Log, _Server) .Delete(site.ApplicationDefaults.ApplicationPoolName); exists = false; } else { exists = true; return(site); } } else { exists = false; } //Create Pool ApplicationPoolManager .Using(_Environment, _Log, _Server) .Create(settings.ApplicationPool); //Site Settings site = _Server.Sites.Add( settings.Name, this.GetPhysicalDirectory(settings), settings.Binding.Port); if (!String.IsNullOrEmpty(settings.AlternateEnabledProtocols)) { site.ApplicationDefaults.EnabledProtocols = settings.AlternateEnabledProtocols; } site.Bindings.Clear(); var binding = site.Bindings.CreateElement(); binding.Protocol = settings.Binding.BindingProtocol.ToString().ToLower(); binding.BindingInformation = settings.Binding.BindingInformation; if (settings.Binding.CertificateHash != null) { binding.CertificateHash = settings.Binding.CertificateHash; } if (!String.IsNullOrEmpty(settings.Binding.CertificateStoreName)) { binding.CertificateStoreName = settings.Binding.CertificateStoreName; } site.Bindings.Add(binding); site.ServerAutoStart = settings.ServerAutoStart; site.ApplicationDefaults.ApplicationPoolName = settings.ApplicationPool.Name; // Security var serverType = settings is WebsiteSettings ? "webServer" : "ftpServer"; var hostConfig = GetWebConfiguration(); hostConfig.SetAuthentication(serverType, settings.Name, "", settings.Authentication, _Log); hostConfig.SetAuthorization(serverType, settings.Name, "", settings.Authorization); return(site); }
protected void SetAuthorization(SiteSettings settings) { if (settings.Authorization != null) { //Get Type string server = ""; if (settings is WebsiteSettings) { server = "webServer"; } else { server = "ftpServer"; } //Authorization var config = _Server.GetApplicationHostConfiguration(); var authorization = config.GetSection("system." + server + "/security/authorization", settings.Name); var authCollection = authorization.GetCollection(); var addElement = authCollection.CreateElement("add"); addElement.SetAttributeValue("accessType", "Allow"); switch (settings.Authorization.AuthorizationType) { case AuthorizationType.AllUsers: addElement.SetAttributeValue("users", "*"); break; case AuthorizationType.SpecifiedUser: addElement.SetAttributeValue("users", string.Join(", ", settings.Authorization.Users)); break; case AuthorizationType.SpecifiedRoleOrUserGroup: addElement.SetAttributeValue("roles", string.Join(", ", settings.Authorization.Roles)); break; } //Permissions var permissions = new List<string>(); if (settings.Authorization.CanRead) { permissions.Add("Read"); } if (settings.Authorization.CanWrite) { permissions.Add("Write"); } addElement.SetAttributeValue("permissions", string.Join(", ", permissions)); authCollection.Clear(); authCollection.Add(addElement); _Log.Information("Windows Authentication enabled: {0}", settings.Authentication.EnableWindowsAuthentication); } }
protected Site CreateSite(SiteSettings settings, out bool exists) { if (settings == null) { throw new ArgumentNullException("settings"); } if (string.IsNullOrWhiteSpace(settings.Name)) { throw new ArgumentException("Site name cannot be null!"); } if (string.IsNullOrWhiteSpace(settings.HostName)) { throw new ArgumentException("Host name cannot be null!"); } //Get Site Site site = _Server.Sites.FirstOrDefault(p => p.Name == settings.Name); if (site != null) { _Log.Information("Site '{0}' already exists.", settings.Name); if (settings.Overwrite) { _Log.Information("Site '{0}' will be overriden by request.", settings.Name); this.Delete(settings.Name); ApplicationPoolManager .Using(_Environment, _Log, _Server) .Delete(site.ApplicationDefaults.ApplicationPoolName); exists = false; } else { exists = true; return site; } } else { exists = false; } //Create Pool ApplicationPoolManager .Using(_Environment, _Log, _Server) .Create(settings.ApplicationPool); //Site Settings site = _Server.Sites.Add( settings.Name, settings.BindingProtocol.ToString().ToLower(), settings.BindingInformation, this.GetPhysicalDirectory(settings)); if (settings.CertificateHash != null) { site.Bindings[0].CertificateHash = settings.CertificateHash; } if (!String.IsNullOrEmpty(settings.CertificateStoreName)) { site.Bindings[0].CertificateStoreName = settings.CertificateStoreName; } site.ServerAutoStart = settings.ServerAutoStart; site.ApplicationDefaults.ApplicationPoolName = settings.ApplicationPool.Name; //Security this.SetAuthentication(settings); this.SetAuthorization(settings); return site; }
protected void SetAuthentication(SiteSettings settings) { if (settings.Authentication != null) { //Get Type string server = ""; if (settings is WebsiteSettings) { server = "webServer"; } else { server = "ftpServer"; } //Authentication var config = _Server.GetApplicationHostConfiguration(); var authentication = config.GetSection("system." + server + "/security/authorization", settings.Name); // Anonymous Authentication var anonymousAuthentication = authentication.GetChildElement("anonymousAuthentication"); anonymousAuthentication.SetAttributeValue("enabled", settings.Authentication.EnableAnonymousAuthentication); _Log.Information("Anonymous Authentication enabled: {0}", settings.Authentication.EnableAnonymousAuthentication); // Basic Authentication var basicAuthentication = authentication.GetChildElement("basicAuthentication"); basicAuthentication.SetAttributeValue("enabled", settings.Authentication.EnableBasicAuthentication); basicAuthentication.SetAttributeValue("userName", settings.Authentication.Username); basicAuthentication.SetAttributeValue("password", settings.Authentication.Password); _Log.Information("Basic Authentication enabled: {0}", settings.Authentication.EnableBasicAuthentication); // Windows Authentication var windowsAuthentication = authentication.GetChildElement("windowsAuthentication"); windowsAuthentication.SetAttributeValue("enabled", settings.Authentication.EnableWindowsAuthentication); _Log.Information("Windows Authentication enabled: {0}", settings.Authentication.EnableWindowsAuthentication); } }