/// <summary> /// Assigns a certificate to a website binding /// </summary> /// <param name="siteInformation">Information identifying the website and binding to assign the certificate to</param> public static void AssignCertificateToSite(SiteInformation siteInformation) { if (siteInformation == null) { throw new ArgumentNullException("siteInformation"); } Binding binding = GetBinding(siteInformation); if (string.IsNullOrEmpty(siteInformation.CertificateHash)) { throw new CertificateException("Certificate hash is required when adding certificate."); } ConfigurationMethod configurationMethod = binding.Methods.FirstOrDefault(x => string.Equals(x.Name, "AddSslCertificate", StringComparison.InvariantCultureIgnoreCase)); if (configurationMethod == null) { throw new CertificateException("Unable to access the AddSslCertificate configuration method."); } ConfigurationMethodInstance configurationMethodInstance = configurationMethod.CreateInstance(); configurationMethodInstance.Input.SetAttributeValue("certificateHash", siteInformation.CertificateHash); configurationMethodInstance.Input.SetAttributeValue("certificateStoreName", siteInformation.CertificateStore); configurationMethodInstance.Execute(); }
/// <summary> /// Marks a server as unhealthy /// </summary> /// <param name="farm">The name of the WebFarm</param> /// <param name="address">The address of the server</param> public void SetServerUnhealthy(string farm, string address) { ConfigurationElement arrElement = this.GetServerArr(farm, address); if (arrElement != null) { ConfigurationMethod method = arrElement.Methods["SetUnhealthy"]; ConfigurationMethodInstance instance = method.CreateInstance(); instance.Execute(); _Log.Information("Marking the server '{0}' as unhealthy.", address); } }
/// <summary> /// Marks a server as unavailable /// </summary> /// <param name="farm">The name of the WebFarm</param> /// <param name="address">The address of the server</param> public void SetServerUnavailable(string farm, string address) { ConfigurationElement arrElement = this.GetServerArr(farm, address); if (arrElement != null) { ConfigurationMethod method = arrElement.Methods["SetState"]; ConfigurationMethodInstance instance = method.CreateInstance(); instance.Input.Attributes[0].Value = 3; instance.Execute(); _Log.Information("Marking the server '{0}' as unavailable.", address); } }
/// <summary> /// Unassigns a certificate to a website binding /// </summary> /// <param name="siteInformation">Information identifying the website and binding to assign the certificate to</param> public static void UnassignCertificateFromSite(SiteInformation siteInformation) { if (siteInformation == null) { throw new ArgumentNullException("siteInformation"); } Binding binding = GetBinding(siteInformation); ConfigurationMethod configurationMethod = binding.Methods.FirstOrDefault(x => string.Equals(x.Name, "RemoveSslCertificate", StringComparison.InvariantCultureIgnoreCase)); if (configurationMethod == null) { throw new CertificateException("Unable to access the RemoveSslCertificate configuration method."); } ConfigurationMethodInstance configurationMethodInstance = configurationMethod.CreateInstance(); configurationMethodInstance.Execute(); }
static void Main(string[] args) { try { string certHash = ""; string certStore = "MY"; X509Store certificateStore = new X509Store(StoreLocation.LocalMachine); certificateStore.Open(OpenFlags.ReadOnly); foreach (var certificate in certificateStore.Certificates) { bool isServerAuth = false; Console.WriteLine("[Info] " + certificate.GetCertHashString()); foreach (X509Extension extension in certificate.Extensions) { Console.WriteLine(extension.Format(true)); Console.WriteLine(extension.Oid.Value.ToString()); if (extension.Format(true).Contains("Server Authentication")) { isServerAuth = true; } if (certificate.Verify() && isServerAuth) { certHash = certificate.GetCertHashString(); } else if (certificate.Verify() && isServerAuth) { certHash = certificate.GetCertHashString(); } else if (certificate.Verify()) { certHash = certificate.GetCertHashString(); } } } certificateStore.Close(); ServerManager mgr = null; string server = null; // or remote machine name string siteName = "Default Web Site"; string bindingProtocol = "https"; string bindingInfo = "*:7443:"; if (String.IsNullOrEmpty(server)) { mgr = new ServerManager(); } else { mgr = ServerManager.OpenRemote(server); } Site site = mgr.Sites[siteName]; Binding binding = null; foreach (Binding b in site.Bindings) { Console.WriteLine(bindingInfo); if (b.Protocol.Equals(bindingProtocol) && b.BindingInformation.Equals(bindingInfo)) { binding = b; break; } } if (binding == null) { throw new Exception("Binding not found!"); } if (!String.IsNullOrEmpty(certHash)) { ConfigurationMethod method = binding.Methods["AddSslCertificate"]; if (method == null) { throw new Exception("Unable to access the AddSslCertificate configuration method"); } ConfigurationMethodInstance mi = method.CreateInstance(); mi.Input.SetAttributeValue("certificateHash", certHash); mi.Input.SetAttributeValue("certificateStoreName", certStore); mi.Execute(); Console.WriteLine("Certificate has been added: " + certHash); } else { Console.WriteLine("Certificate can not be found"); } } catch (Exception e) { Console.WriteLine("{0} Exception caught.", e); Console.Read(); Environment.Exit(-1); } Thread.Sleep(9000); }