private static IEnumerable <Binding> GetBindings(Microsoft.Web.Administration.Site iisSite) { var allowedProtocols = new[] { "http", "https" }; var certificates = GetCertificates(); foreach (var binding in iisSite.Bindings.Where(x => allowedProtocols.Contains(x.Protocol))) { var servantBinding = new Binding(); if (binding.Protocol == "https") { if (binding.CertificateHash == null) { continue; } var certificate = certificates.SingleOrDefault(cert => cert.Hash.SequenceEqual(binding.CertificateHash)); if (certificate != null) { servantBinding.CertificateName = certificate.Name; servantBinding.CertificateThumbprint = certificate.Thumbprint; } else { continue; } } servantBinding.Protocol = (Protocol)Enum.Parse(typeof(Protocol), binding.Protocol); servantBinding.Hostname = binding.Host; servantBinding.Port = binding.EndPoint.Port; var endPointAddress = binding.EndPoint.Address.ToString(); servantBinding.IpAddress = endPointAddress == "0.0.0.0" ? "*" : endPointAddress; yield return(servantBinding); } }
public void ValidateSite(ref Site site) { string[] bindingsUserInputs = Request.Form.BindingsUserInput.ToString().Split(','); string[] bindingsCertificateThumbprint = Request.Form.BindingsCertificateThumbprint.ToString().Split(','); string[] bindingsIpAddresses = Request.Form.BindingsIpAddress.ToString().Split(','); site.Bindings = new List<Binding>(); var certificates = SiteManager.GetCertificates(); for (var i = 0; i < bindingsUserInputs.Length; i++) { if (string.IsNullOrWhiteSpace(bindingsUserInputs[i])) continue; var isValid = true; var userinput = bindingsUserInputs[i]; var finalizedHost = BindingHelper.SafeFinializeBinding(userinput); var ip = bindingsIpAddresses[i]; if (string.IsNullOrWhiteSpace(ip)) ip = "*"; if (finalizedHost == null) { AddPropertyError("bindingsuserinput[" + i + "]", "The binding is invalid."); isValid = false; } else if (!BindingHelper.IsIpValid(ip)) { AddPropertyError("bindingsipaddress[" + i + "]", string.Format("The IP {0} is not valid.", ip)); isValid = false; } else if (SiteManager.IsBindingInUse(finalizedHost, bindingsIpAddresses[i], site.IisId)) { AddPropertyError("bindingsuserinput[" + i + "]", string.Format("The binding {0} is already in use.", finalizedHost)); isValid = false; } Binding binding; if (isValid) { var certificate = certificates.SingleOrDefault(x => x.Thumbprint == bindingsCertificateThumbprint[i]); binding = BindingHelper.ConvertToBinding(finalizedHost, ip, certificate); } else { binding = new Binding() { CertificateName = bindingsCertificateThumbprint[i], UserInput = bindingsUserInputs[i], IpAddress = ip }; } site.Bindings.Add(binding); } if (!site.Bindings.Any()) { AddPropertyError("bindingsipaddress[0]", "Minimum one binding is required."); site.Bindings.Add(new Binding() { UserInput = "" }); } if (string.IsNullOrWhiteSpace(site.Name)) AddPropertyError("name", "Name is required."); var existingSite = SiteManager.GetSiteByName(site.Name); if (site.Name != null && existingSite != null && existingSite.IisId != site.IisId) AddPropertyError("name", "There's already a site with this name."); if (string.IsNullOrWhiteSpace(site.SitePath)) AddPropertyError("sitepath", "Site path is required."); else { if (!FileSystemHelper.IsPathValid(site.SitePath)) { AddPropertyError("sitepath", "Path cannot contain the following characters: ?, ;, :, @, &, =, +, $, ,, |, \", <, >, *."); } else { if (!FileSystemHelper.DirectoryExists(site.SitePath)) { FileSystemHelper.CreateDirectory(site.SitePath); } } } }
public static bool IsBindingInUse(Binding binding, int iisSiteId = 0) { var bindingInformations = ConvertBindingsToBindingInformations(new[] { binding }); return(GetBindingInUse(iisSiteId, bindingInformations.ToList()) != null); }
public static bool IsBindingInUse(Binding binding, int iisSiteId = 0) { var bindingInformations = ConvertBindingsToBindingInformations(new[] {binding}); return GetBindingInUse(iisSiteId, bindingInformations.ToList()) != null; }
private static IEnumerable<Binding> GetBindings(Microsoft.Web.Administration.Site iisSite) { var allowedProtocols = new[] { "http", "https" }; var certificates = GetCertificates(); foreach (var binding in iisSite.Bindings.Where(x => allowedProtocols.Contains(x.Protocol))) { var servantBinding = new Binding(); if (binding.Protocol == "https") { if(binding.CertificateHash == null) continue; var certificate = certificates.SingleOrDefault(cert => cert.Hash.SequenceEqual(binding.CertificateHash)); if (certificate != null) { servantBinding.CertificateName = certificate.Name; servantBinding.CertificateThumbprint = certificate.Thumbprint; } else continue; } servantBinding.Protocol = (Protocol) Enum.Parse(typeof(Protocol), binding.Protocol); servantBinding.Hostname = binding.Host; servantBinding.Port = binding.EndPoint.Port; var endPointAddress = binding.EndPoint.Address.ToString(); servantBinding.IpAddress = endPointAddress == "0.0.0.0" ? "*" : endPointAddress; yield return servantBinding; } }