void IInstallAction.Run(SetupVariables vars) { var siteName = vars.ComponentFullName; var ip = vars.WebSiteIP; var port = vars.WebSitePort; var domain = vars.WebSiteDomain; var contentPath = vars.InstallationFolder; var iisVersion = vars.IISVersion; var iis7 = (iisVersion.Major == 7); var userName = CreateWebApplicationPoolAction.GetWebIdentity(vars); var userPassword = vars.UserPassword; var appPool = vars.WebApplicationPoolName; var componentId = vars.ComponentId; var newSiteId = String.Empty; // Begin(LogStartMessage); // Log.WriteStart(LogStartMessage); // Log.WriteInfo(String.Format("Creating web site \"{0}\" ( IP: {1}, Port: {2}, Domain: {3} )", siteName, ip, port, domain)); //check for existing site var oldSiteId = iis7 ? WebUtils.GetIIS7SiteIdByBinding(ip, port, domain) : WebUtils.GetSiteIdByBinding(ip, port, domain); // if (oldSiteId != null) { // get site name string oldSiteName = iis7 ? oldSiteId : WebUtils.GetSite(oldSiteId).Name; throw new Exception( String.Format("'{0}' web site already has server binding ( IP: {1}, Port: {2}, Domain: {3} )", oldSiteName, ip, port, domain)); } // create site var site = new WebSiteItem { Name = siteName, SiteIPAddress = ip, ContentPath = contentPath, AllowExecuteAccess = false, AllowScriptAccess = true, AllowSourceAccess = false, AllowReadAccess = true, AllowWriteAccess = false, AnonymousUsername = userName, AnonymousUserPassword = userPassword, AllowDirectoryBrowsingAccess = false, AuthAnonymous = true, AuthWindows = true, DefaultDocs = null, HttpRedirect = "", InstalledDotNetFramework = AspNetVersion.AspNet20, ApplicationPool = appPool, // Bindings = new ServerBinding[] { new ServerBinding(ip, port, domain) }, }; // create site if (iis7) { newSiteId = WebUtils.CreateIIS7Site(site); } else { newSiteId = WebUtils.CreateSite(site); } vars.VirtualDirectory = String.Empty; vars.NewWebSite = true; vars.NewVirtualDirectory = false; // update setup variables vars.WebSiteId = newSiteId; //update log Log.WriteEnd("Created web site"); // Finish(LogStartMessage); //update install log InstallLog.AppendLine(string.Format("- Created a new web site named \"{0}\" ({1})", siteName, newSiteId)); InstallLog.AppendLine(" You can access the application by the following URLs:"); string[] urls = Utils.GetApplicationUrls(ip, domain, port, null); foreach (string url in urls) { InstallLog.AppendLine(" http://" + url); } }
private void CreateSite(string siteName, string ip, string port, string domain, string contentPath, string userName, string userPassword, string appPool) { SetProgressText("Creating web site..."); Log.WriteStart("Creating web site"); Log.WriteInfo(string.Format("Creating web site \"{0}\" ( IP: {1}, Port: {2}, Domain: {3} )", siteName, ip, port, domain)); Version iisVersion = Wizard.SetupVariables.IISVersion; bool iis7 = (iisVersion.Major == 7); //check for existing site string oldSiteId = iis7 ? WebUtils.GetIIS7SiteIdByBinding(ip, port, domain) : WebUtils.GetSiteIdByBinding(ip, port, domain); if (oldSiteId != null) { // get site name string oldSiteName = iis7 ? oldSiteId : WebUtils.GetSite(oldSiteId).Name; throw new Exception( String.Format("'{0}' web site already has server binding ( IP: {1}, Port: {2}, Domain: {3} )", oldSiteName, ip, port, domain)); } // create site WebSiteItem site = new WebSiteItem(); site.Name = siteName; site.SiteIPAddress = ip; site.ContentPath = contentPath; //site.LogFileDirectory = logsPath; //set bindings ServerBinding binding = new ServerBinding(ip, port, domain); site.Bindings = new ServerBinding[] { binding }; // set other properties site.AllowExecuteAccess = false; site.AllowScriptAccess = true; site.AllowSourceAccess = false; site.AllowReadAccess = true; site.AllowWriteAccess = false; site.AnonymousUsername = userName; site.AnonymousUserPassword = userPassword; site.AllowDirectoryBrowsingAccess = false; site.AuthAnonymous = true; site.AuthWindows = true; site.DefaultDocs = null; // inherit from service site.HttpRedirect = ""; site.InstalledDotNetFramework = AspNetVersion.AspNet20; site.ApplicationPool = appPool; // create site string newSiteId = iis7 ? WebUtils.CreateIIS7Site(site) : WebUtils.CreateSite(site); //add rollback action if (iis7) RollBack.RegisterIIS7WebSiteAction(newSiteId); else RollBack.RegisterWebSiteAction(newSiteId); // update config setings string componentId = Wizard.SetupVariables.ComponentId; AppConfig.SetComponentSettingStringValue(componentId, "WebSiteId", newSiteId); AppConfig.SetComponentSettingStringValue(componentId, "WebSiteIP", ip); AppConfig.SetComponentSettingStringValue(componentId, "WebSitePort", port); AppConfig.SetComponentSettingStringValue(componentId, "WebSiteDomain", domain); AppConfig.SetComponentSettingStringValue(componentId, "VirtualDirectory", string.Empty); AppConfig.SetComponentSettingBooleanValue(componentId, "NewWebSite", true); AppConfig.SetComponentSettingBooleanValue(componentId, "NewVirtualDirectory", false); // update setup variables Wizard.SetupVariables.WebSiteId = newSiteId; //update log Log.WriteEnd("Created web site"); //update install log InstallLog.AppendLine(string.Format("- Created a new web site named \"{0}\" ({1})", siteName, newSiteId)); InstallLog.AppendLine(" You can access the application by the following URLs:"); string[] urls = GetApplicationUrls(ip, domain, port, null); foreach (string url in urls) { InstallLog.AppendLine(" http://" + url); } }
/// <summary> /// Retrieves web sites. /// </summary> /// <returns>Web sites.</returns> internal static WebSiteItem[] GetWebSites() { List<WebSiteItem> sites = new List<WebSiteItem>(); // get all sites ManagementObjectCollection objSites = wmi.ExecuteQuery("SELECT * FROM IIsWebServerSetting"); foreach(ManagementObject objSite in objSites) { WebSiteItem site = new WebSiteItem(); FillWebSiteFromWmiObject(site, objSite); sites.Add(site); } return sites.ToArray(); }
/// <summary> /// Creates site. /// </summary> /// <param name="site">Site object.</param> /// <returns>Site id.</returns> internal static string CreateIIS7Site(WebSiteItem site) { ServerManager serverManager = new ServerManager(); Site webSite = serverManager.Sites[site.Name]; if ( webSite == null ) webSite = serverManager.Sites.Add(site.Name, site.ContentPath, 80); // cleanup all bindings webSite.Bindings.Clear(); // foreach (ServerBinding binding in site.Bindings) { // webSite.Bindings.Add(binding.IP + ":" + binding.Port + ":" + binding.Host, Uri.UriSchemeHttp); } // webSite.Applications[0].ApplicationPoolName = site.ApplicationPool; // //webSite.LogFile.Directory = site.LogFileDirectory; // site.SiteId = webSite.Name; // webSite.ServerAutoStart = true; //authentication Configuration cnfg = serverManager.GetApplicationHostConfiguration(); ConfigurationSection section = cnfg.GetSection("system.webServer/security/authentication/anonymousAuthentication", site.Name); section["enabled"] = site.AuthAnonymous; section["userName"] = string.Empty; section["password"] = string.Empty; section = cnfg.GetSection("system.webServer/security/authentication/windowsAuthentication", site.Name); section["enabled"] = site.AuthWindows; //TODO: default documents serverManager.CommitChanges(); return site.SiteId; }
/// <summary> /// Creates site. /// </summary> /// <param name="site">Site object.</param> /// <returns>Site id.</returns> internal static string CreateSite(WebSiteItem site) { //CheckWebServerBindings(site.Bindings); // set folder permissions //SetWebFolderPermissions(site.ContentPath, site.AnonymousUsername); // create Web site ManagementObject objService = wmi.GetObject(String.Format("IIsWebService='{0}'", IIS_SERVICE_ID)); ManagementBaseObject methodParams = objService.GetMethodParameters("CreateNewSite"); // create server bindings ManagementClass clsBinding = wmi.GetClass("ServerBinding"); ManagementObject[] objBindings = new ManagementObject[site.Bindings.Length]; for(int i = 0; i < objBindings.Length; i++) { objBindings[i] = clsBinding.CreateInstance(); objBindings[i]["Hostname"] = site.Bindings[i].Host; objBindings[i]["IP"] = site.Bindings[i].IP; objBindings[i]["Port"] = site.Bindings[i].Port; } methodParams["ServerBindings"] = objBindings; methodParams["ServerComment"] = site.Name; methodParams["PathOfRootVirtualDir"] = site.ContentPath; ManagementBaseObject objResult = objService.InvokeMethod("CreateNewSite", methodParams, new InvokeMethodOptions()); // get WEB settings string siteId = ((string)objResult["returnValue"]).Remove(0, "IIsWebServer='".Length).Replace("'",""); // update site properties ManagementObject objSite = wmi.GetObject(String.Format("IIsWebServerSetting='{0}'", siteId)); ManagementObject objVirtDir = wmi.GetObject( String.Format("IIsWebVirtualDirSetting='{0}'", GetVirtualDirectoryPath(siteId, ""))); if(site.LogFileDirectory != null && site.LogFileDirectory != "") objSite.Properties["LogFileDirectory"].Value = site.LogFileDirectory; FillWmiObjectFromVirtualDirectory(objSite, site); objSite.Put(); FillWmiObjectFromVirtualDirectory(objVirtDir, site); FillWmiObjectFromVirtualDirectoryRest(objVirtDir, site); // set correct default documents SetVirtualDirectoryDefaultDocs(objVirtDir); // set ASP.NET SetVirtualDirectoryAspNetMappings(objVirtDir, site.InstalledDotNetFramework); // save object objVirtDir.Put(); // start site ChangeSiteState(siteId, ServerState.Started); return siteId; }
private static void FillWebSiteFromWmiObject(WebSiteItem site, ManagementBaseObject obj) { site.SiteId = (string)obj.Properties["Name"].Value; site.Name = (string)obj.Properties["ServerComment"].Value; // get server bindings ManagementBaseObject[] objBindings = ((ManagementBaseObject[])obj.Properties["ServerBindings"].Value); if(objBindings != null) { site.Bindings = new ServerBinding[objBindings.Length]; for(int i = 0; i < objBindings.Length; i++) { site.Bindings[i] = new ServerBinding( (string)objBindings[i].Properties["IP"].Value, (string)objBindings[i].Properties["Port"].Value, (string)objBindings[i].Properties["Hostname"].Value); } } }
/// <summary> /// Retreives site by site id. /// </summary> /// <param name="siteId">Site id.</param> /// <returns>Site object.</returns> internal static WebSiteItem GetSite(string siteId) { WebSiteItem site = new WebSiteItem(); // get web server settings object ManagementObject objSite = wmi.GetObject(String.Format("IIsWebServerSetting='{0}'", siteId)); FillWebSiteFromWmiObject(site, objSite); // get ROOT vritual directory settings object ManagementObject objVirtDir = wmi.GetObject( String.Format("IIsWebVirtualDirSetting='{0}'", GetVirtualDirectoryPath(siteId, ""))); FillVirtualDirectoryFromWmiObject(site, objVirtDir); FillVirtualDirectoryRestFromWmiObject(site, objVirtDir); return site; }