How to get a list of SSL sites and certificates from IIS 6.0 using C #, WMI and / or System.Management?

I am trying to export all SSL certificates on IIS 6.0 sites from a specific remote server to a centralized backup server so that we can transfer and / or copy our SSL certificates, however I cannot figure out how to do this with IIS 6.0 (all our servers they are still launching IIS 6.0 in the setup and production phase). Is there a way to do C # and System.Management to target IIS 6.0 websites. I tried everything I could think of.

Pseduo Logic: Get a List of All IIS Websites on Server X If the site has an associated SSL certificate binding, export the SSL certificate with the name of the IIS website.

Here is the code that is closer to what I need for IIS 7.0:

using (ServerManager serverManager = ServerManager.OpenRemote(this.ServerName)) { string collectionDisplay = null; if (serverManager.Sites != null) collectionDisplay = "There are " + serverManager.Sites.Count.ToString() + " sites:\n\n"; string siteDisplay = null; foreach (Site site in serverManager.Sites) { siteDisplay = siteDisplay + site.Name + ": ID = " + site.Id + "\n"; // Display each property of each bindings. string bindingDisplay = null; foreach (Binding binding in site.Bindings) { if (binding.Protocol == "https") { bindingDisplay = bindingDisplay + " Binding:\n BindingInformation: " + binding.BindingInformation; // There is a CertificateHash and CertificateStoreName for the https protocol only. bindingDisplay = bindingDisplay + "\n CertificateHash: " + binding.CertificateHash + ": "; //Add the certificate hash to the collection if (!IisCertificateHashCollection.ContainsKey(binding.CertificateHash)) { IisCertificateHashCollection.Add(binding.CertificateHash, site.Name); //IisCertificateHashCollection.Add(new KeyValuePair<string, byte[]>(site.Name, binding.CertificateHash)); } // Display the hash. foreach (System.Byte certhashbyte in binding.CertificateHash) { bindingDisplay = bindingDisplay + certhashbyte.ToString() + " "; } bindingDisplay = bindingDisplay + "\n CertificateStoreName: " + binding.CertificateStoreName; } bindingDisplay = bindingDisplay + "\n EndPoint: " + binding.EndPoint; bindingDisplay = bindingDisplay + "\n Host: " + binding.Host; bindingDisplay = bindingDisplay + "\n IsIPPortHostBinding: " + binding.IsIPPortHostBinding; bindingDisplay = bindingDisplay + "\n Protocol: " + binding.Protocol; bindingDisplay = bindingDisplay + "\n ToString: " + binding.ToString(); bindingDisplay = bindingDisplay + "\n UseDsMapper: " + binding.UseDsMapper + "\n\n"; } siteDisplay = siteDisplay + bindingDisplay; } collectionDisplay = collectionDisplay + siteDisplay + "\n"; } 

Here is the code that I can’t get / don’t know how to get the necessary information from IIS 6.0, I can’t get the correct request:

  // Connection succeeds, so there is no issue with that (left out code for that in sample) ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\cimv2", serverName, options)); //ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\MicrosoftIISV2", serverName, options)); scope.Connect(); ObjectQuery oq = new ObjectQuery(@"SELECT * FROM Win32_NTDomain"); ManagementObjectSearcher query = new ManagementObjectSearcher(scope, oq); ManagementObjectCollection queryCollection = query.Get(); foreach (ManagementObject mo in queryCollection) { foreach (PropertyData pd in mo.Properties) { } } 
+4
source share
1 answer

You can use System.DirectoryServices to get the certificate hash in IIS6:

 DirectoryEntry dir = new DirectoryEntry(@"IIS://Localhost/W3SVC/1"); //this is the metabase path PropertyValueCollection vals = dir.Properties[SSLCertHash]; //this is the propertyName 

The rest is the same as in IIS7.

Hope this helps, Rotem Varon

0
source

All Articles