public void CreateWebSiteWithBinding(WebSite webSite, Binding binding)
        {
            binding.WebSiteId = webSite.Id;

            this.webSiteTable.AddEntity(webSite.ToRow());
            this.bindingTable.AddEntity(binding.ToRow());
        }
        public ActionResult Create(WebSiteCreateModel model)
        {
            try
            {
                if (!this.ValidateDuplicatedSites(model.Name))
                {
                    model.Certificates = this.GetCertificatesList();
                    return View(model);
                }

                if (!this.ValidateDuplicatedBinding(model.HostName, model.Protocol, model.Port))
                {
                    model.Certificates = this.GetCertificatesList();
                    return View(model);
                }

                if (this.ValidateCertificateAndPort(model.CertificateId, model.Port, model.Protocol))
                {
                    var webSite = new WebSite()
                    {
                        Name = model.Name.Replace(" ", string.Empty).ToLowerInvariant(),
                        Description = model.Description,
                        EnableCDNChildApplication = model.EnableCDNChildApplication,
                        EnableTestChildApplication = model.EnableTestChildApplication
                    };

                    var binding = new Binding()
                    {
                        Port = model.Port,
                        Protocol = model.Protocol,
                        HostName = model.HostName,
                        IpAddress = model.IpAddress,
                        CertificateId = model.CertificateId
                    };

                    this.webSiteRepository.CreateWebSiteWithBinding(webSite, binding);

                    return RedirectToAction("Index");
                }
                else
                {
                    model.Certificates = this.GetCertificatesList();
                    return View(model);
                }
            }
            catch
            {
                model.Certificates = this.GetCertificatesList();
                return View(model);
            }
        }
        public void UpdateSitesAddingBindings()
        {
            var contosoWebSite = new WebSite
            {
                Name = contosoWebSiteName,
                Bindings = new List<Binding>
                {
                    new Binding { Protocol = "http", IpAddress = "10.0.0.1", Port = 8081, HostName = "contoso.com" }
                }
            };

            var iisManager = new IISManager(localSitesPath, tempSitesPath, null);
            var sites = new List<WebSite> { contosoWebSite };

            iisManager.UpdateSites(sites);

            var contoso = RetrieveWebSite(contosoWebSiteName);

            Assert.IsNotNull(contoso);
            Assert.AreEqual<string>(contosoWebSite.Name, contoso.Name);
            Assert.AreEqual<int>(contosoWebSite.Bindings.Count(), contoso.Bindings.Count);

            // Add a new binding (https)
            var contosoBindings = contosoWebSite.Bindings.ToList();
            contosoBindings.Add(new Binding { Protocol = "https", IpAddress = "10.0.0.1", Port = 8443, CertificateThumbprint = "12345" });
            contosoWebSite.Bindings = contosoBindings;

            iisManager.UpdateSites(sites);

            // Asserts
            Assert.AreEqual<int>(sites.Count, RetrieveWebSites().Count());

            contoso = RetrieveWebSite(contosoWebSiteName);

            Assert.IsNotNull(contoso);
            Assert.AreEqual<string>(contosoWebSite.Name, contoso.Name);
            Assert.AreEqual<int>(2, contoso.Bindings.Count);

            Assert.AreEqual<string>(contosoWebSite.Bindings.First().HostName, contoso.Bindings.First().Host);
            Assert.AreEqual<string>(contosoWebSite.Bindings.First().Protocol, contoso.Bindings.First().Protocol);
            Assert.AreEqual<string>(contosoWebSite.Bindings.First().IpAddress, contoso.Bindings.First().EndPoint.Address.ToString());
            Assert.AreEqual<int>(contosoWebSite.Bindings.First().Port, contoso.Bindings.First().EndPoint.Port);
            Assert.IsNull(contoso.Bindings.First().CertificateHash);

            Assert.IsTrue(string.IsNullOrEmpty(contoso.Bindings.Last().Host));
            Assert.AreEqual<string>(contosoWebSite.Bindings.Last().Protocol, contoso.Bindings.Last().Protocol);
            Assert.AreEqual<string>(contosoWebSite.Bindings.Last().IpAddress, contoso.Bindings.Last().EndPoint.Address.ToString());
            Assert.AreEqual<int>(contosoWebSite.Bindings.Last().Port, contoso.Bindings.Last().EndPoint.Port);
            Assert.AreEqual<string>(StoreName.My.ToString().ToUpperInvariant(), contoso.Bindings.Last().CertificateStoreName.ToUpperInvariant());
            Assert.IsNotNull(contoso.Bindings.Last().CertificateHash);
        }
        public void CreateNewWebSite()
        {
            var site = new WebSite()
            {
                Name = "Test Web Site",
                Description = "Description Test Web Site"
            };

            this.repository.CreateWebSite(site);

            string id = site.Id.ToString();

            WebSiteRow newsite = this.webSiteTable.Query.Where(t => t.RowKey == id).FirstOrDefault();

            Assert.IsNotNull(newsite);
            this.webSiteTable.DeleteEntity(newsite);
        }
        private WebSite CreateWebSiteWithBindings(int nbindings)
        {
            Guid id = Guid.NewGuid();
            var bindings = new List<Binding>();

            var site = new WebSite(id)
            {
                Name = "Test Web Site " + id.ToString(),
                Description = "Description Test Web Site " + id.ToString()
            };

            var binding = new Binding()
            {
                Protocol = "http",
                IpAddress = string.Empty,
                Port = 80,
                HostName = "www.test0.com"
            };

            this.repository.CreateWebSiteWithBinding(site, binding);
            bindings.Add(binding);

            for (int k = 1; k < nbindings; k++)
            {
                var otherBinding = new Binding()
                {
                    Protocol = "http",
                    IpAddress = string.Empty,
                    Port = 80 + k,
                    HostName = string.Format("www.test{0}.com", k)
                };

                this.repository.AddBindingToWebSite(site.Id, otherBinding);
                bindings.Add(otherBinding);
            }

            site.Bindings = bindings;

            return site;
        }
        private string GetDefaultBindingUrl(WebSite webSite)
        {
            var defaultBinding = webSite.Bindings.FirstOrDefault();
            if (defaultBinding == null)
            {
                return null;
            }

            return this.GetUrl(defaultBinding.Protocol, defaultBinding.HostName, defaultBinding.Port);
        }
        private string GetDefaultBindingTestUrl(WebSite webSite)
        {
            if (!webSite.EnableTestChildApplication)
            {
                return null;
            }

            var defaultBinding = webSite.Bindings.FirstOrDefault();
            if (defaultBinding == null)
            {
                return null;
            }

            return this.GetTestUrl(Request.Url.Scheme + "://" + Request.Url.Authority, webSite.Name);
        }
        public ActionResult Edit(Guid id, WebSiteModel model)
        {
            try
            {
                var site = new WebSite(id)
                {
                    Name = model.Name,
                    Description = model.Description,
                    EnableTestChildApplication = model.EnableTestChildApplication,
                    EnableCDNChildApplication = model.EnableCDNChildApplication,
                };

                this.webSiteRepository.UpdateWebSite(site);

                return RedirectToAction("Index");
            }
            catch
            {
                return View(model);
            }
        }
 public void CreateWebSite(WebSite webSite)
 {
     this.webSiteTable.AddEntity(webSite.ToRow());
 }
 public void AddBindingToWebSite(WebSite webSite, Binding binding)
 {
     binding.WebSiteId = webSite.Id;
     this.bindingTable.AddEntity(binding.ToRow());
 }
 public void UpdateWebSite(WebSite webSite)
 {
     this.webSiteTable.AddOrUpdateEntity(webSite.ToRow());
 }
        private static void UpdateApplications(WebSite site, ServerManager serverManager, string siteName, string sitePath, ApplicationPool appPool)
        {
            var iisSites = serverManager.Sites;
            var adminSite = iisSites[roleWebSiteName];

            var testApplication = adminSite.Applications.FirstOrDefault(
                app => app.Path.EndsWith("/test/" + siteName, StringComparison.OrdinalIgnoreCase));
            var cdnApplication = adminSite.Applications.FirstOrDefault(
                app => app.Path.EndsWith("/cdn/" + siteName, StringComparison.OrdinalIgnoreCase));

            if (site.EnableTestChildApplication)
            {
                if (testApplication == null)
                {
                    TraceHelper.TraceInformation("IISManager.Adding Test application for site '{0}'", siteName);
                    testApplication = adminSite.Applications.Add("/test/" + siteName, sitePath);
                    testApplication.ApplicationPoolName = appPool.Name;
                }
            }
            else
            {
                if (testApplication != null)
                {
                    TraceHelper.TraceInformation("IISManager.Removing Test application for site '{0}'", siteName);
                    adminSite.Applications.Remove(testApplication);
                }
            }

            if (site.EnableCDNChildApplication)
            {
                if (cdnApplication == null)
                {
                    TraceHelper.TraceInformation("IISManager.Adding CDN application for site '{0}'", siteName);
                    cdnApplication = adminSite.Applications.Add("/cdn/" + siteName, Path.Combine(sitePath, "cdn"));
                    cdnApplication.ApplicationPoolName = appPool.Name;
                }
            }
            else
            {
                if (cdnApplication != null)
                {
                    TraceHelper.TraceInformation("IISManager.Removing CDN application for site '{0}'", siteName);
                    adminSite.Applications.Remove(cdnApplication);
                }
            }
        }
        public void UpdateSitesRemovingBindings()
        {
            var fabrikamWebSite = new WebSite
            {
                Name = fabrikamWebSiteName,
                Bindings = new List<Binding>
                {
                    new Binding { Protocol = "https", IpAddress = "127.0.0.1", Port = 8443, CertificateThumbprint = "12345" },
                    new Binding { Protocol = "http", IpAddress = "127.0.0.1", Port = 8082 }
                }
            };

            var iisManager = new IISManager(localSitesPath, tempSitesPath, null);
            var sites = new List<WebSite> { fabrikamWebSite };

            iisManager.UpdateSites(sites);

            var fabrikam = RetrieveWebSite(fabrikamWebSiteName);

            Assert.IsNotNull(fabrikam);
            Assert.AreEqual<string>(fabrikamWebSite.Name, fabrikam.Name);
            Assert.AreEqual<int>(2, fabrikam.Bindings.Count);

            var fabrikamBindings = fabrikamWebSite.Bindings.ToList();
            fabrikamBindings.RemoveAt(1);
            fabrikamWebSite.Bindings = fabrikamBindings;

            iisManager.UpdateSites(sites);

            // Asserts
            Assert.AreEqual<int>(sites.Count(), RetrieveWebSites().Count());

            fabrikam = RetrieveWebSite(fabrikamWebSiteName);

            Assert.IsNotNull(fabrikam);
            Assert.AreEqual<string>(fabrikamWebSite.Name, fabrikam.Name);
            Assert.AreEqual<int>(1, fabrikam.Bindings.Count);

            Assert.IsTrue(string.IsNullOrEmpty(fabrikam.Bindings.First().Host));
            Assert.AreEqual<string>(fabrikamWebSite.Bindings.First().Protocol, fabrikam.Bindings.First().Protocol);
            Assert.AreEqual<string>(fabrikamWebSite.Bindings.First().IpAddress, fabrikam.Bindings.First().EndPoint.Address.ToString());
            Assert.AreEqual<int>(fabrikamWebSite.Bindings.First().Port, fabrikam.Bindings.First().EndPoint.Port);
            Assert.AreEqual<string>(StoreName.My.ToString().ToUpperInvariant(), fabrikam.Bindings.First().CertificateStoreName.ToUpperInvariant());
            Assert.IsNotNull(fabrikam.Bindings.First().CertificateHash);
        }
        public void UpdateSitesWithInitialBindings()
        {
            var contosoWebSite = new WebSite
            {
                Name = contosoWebSiteName,
                Bindings = new List<Binding>
                {
                    new Binding { Protocol = "http", IpAddress = "*", Port = 8081, HostName = "contoso.com" }
                }
            };

            var fabrikamWebSite = new WebSite
            {
                Name = fabrikamWebSiteName,
                Bindings = new List<Binding>
                {
                    new Binding { Protocol = "https", IpAddress = "*", Port = 8443, CertificateThumbprint = "12354" },
                    new Binding { Protocol = "http", IpAddress = "127.0.0.1", Port = 8082 }
                }
            };

            var iisManager = new IISManager(localSitesPath, tempSitesPath, null);
            var sites = new List<WebSite> { contosoWebSite, fabrikamWebSite };

            iisManager.UpdateSites(sites);

            // Asserts
            Assert.AreEqual<int>(sites.Count, RetrieveWebSites().Count());

            var contoso = RetrieveWebSite(contosoWebSiteName);

            Assert.IsNotNull(contoso);
            Assert.AreEqual<string>(contosoWebSite.Name, contoso.Name);
            Assert.AreEqual<int>(contosoWebSite.Bindings.Count(), contoso.Bindings.Count);

            Assert.AreEqual<string>(contosoWebSite.Bindings.First().HostName, contoso.Bindings.First().Host);
            Assert.AreEqual<string>(contosoWebSite.Bindings.First().Protocol, contoso.Bindings.First().Protocol);
            Assert.AreEqual<string>("0.0.0.0", contoso.Bindings.First().EndPoint.Address.ToString());
            Assert.AreEqual<int>(contosoWebSite.Bindings.First().Port, contoso.Bindings.First().EndPoint.Port);
            Assert.IsNull(contoso.Bindings.First().CertificateHash);

            var fabrikam = RetrieveWebSite(fabrikamWebSiteName);

            Assert.IsNotNull(fabrikam);
            Assert.AreEqual<string>(fabrikamWebSite.Name, fabrikam.Name);
            Assert.AreEqual<int>(fabrikamWebSite.Bindings.Count(), fabrikam.Bindings.Count);

            Assert.IsTrue(string.IsNullOrEmpty(fabrikam.Bindings.First().Host));
            Assert.AreEqual<string>(fabrikamWebSite.Bindings.First().Protocol, fabrikam.Bindings.First().Protocol);
            Assert.AreEqual<string>(string.Empty, fabrikam.Bindings.First().Host);
            Assert.AreEqual<string>("0.0.0.0", fabrikam.Bindings.First().EndPoint.Address.ToString());
            Assert.AreEqual<int>(fabrikamWebSite.Bindings.First().Port, fabrikam.Bindings.First().EndPoint.Port);
            Assert.AreEqual<string>(StoreName.My.ToString().ToUpperInvariant(), fabrikam.Bindings.First().CertificateStoreName.ToUpperInvariant());
            Assert.IsNotNull(fabrikam.Bindings.First().CertificateHash);

            Assert.IsTrue(string.IsNullOrEmpty(fabrikam.Bindings.Last().Host));
            Assert.AreEqual<string>(fabrikamWebSite.Bindings.Last().Protocol, fabrikam.Bindings.Last().Protocol);
            Assert.AreEqual<string>(fabrikamWebSite.Bindings.Last().IpAddress, fabrikam.Bindings.Last().EndPoint.Address.ToString());
            Assert.AreEqual<int>(fabrikamWebSite.Bindings.Last().Port, fabrikam.Bindings.Last().EndPoint.Port);
            Assert.IsNull(fabrikam.Bindings.Last().CertificateHash);
        }
        public void UpdateSitesRemovingSite()
        {
            var contosoWebSite = new WebSite
            {
                Name = contosoWebSiteName,
                Bindings = new List<Binding>
                {
                    new Binding { Protocol = "http", IpAddress = "127.0.0.1", Port = 8081, HostName = "contoso.com" }
                }
            };

            var fabrikamWebSite = new WebSite
            {
                Name = fabrikamWebSiteName,
                Bindings = new List<Binding>
                {
                    new Binding { Protocol = "https", IpAddress = "127.0.0.1", Port = 8443, CertificateThumbprint = "12345" }
                }
            };

            var iisManager = new IISManager(localSitesPath, tempSitesPath, null);
            var sites = new List<WebSite> { contosoWebSite, fabrikamWebSite };

            iisManager.UpdateSites(sites);

            Assert.AreEqual<int>(2, RetrieveWebSites().Count());

            sites.RemoveAt(0);
            iisManager.UpdateSites(sites);

            // Asserts
            Assert.AreEqual<int>(1, RetrieveWebSites().Count());

            var contoso = RetrieveWebSite(contosoWebSiteName);
            var fabrikam = RetrieveWebSite(fabrikamWebSiteName);

            Assert.IsNull(contoso);
            Assert.IsNotNull(fabrikam);
        }