如何使用C#,WMI和/或System.Management从IIS 6.0获取站点和SSL证书列表?

时间:2011-07-11 21:50:13

标签: c# iis-6 wmi system.management

我正在尝试将IIS 6.0站点上的所有SSL证书从特定的远程服务器导出到集中式备份服务器,以便我们可以迁移和/或备份我们的SSL证书,但是我无法弄清楚如何使用IIS 6.0执行此操作(我们在暂存和生产中的所有服务器仍然运行IIS 6.0)。有没有办法使用C#和System.Management来定位IIS 6.0网站。我已经尝试了我能想到的一切。

Pseduo Logic: 获取Server X上所有IIS网站的列表 如果站点具有与之关联的SSL证书绑定,请使用IIS网站的名称导出SSL证书。

这里的代码更接近我对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";

        }

这是我无法获得/不知道如何从IIS 6.0获取所需信息的代码,我无法正确查询:

            // 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)
                {

                }
            }

1 个答案:

答案 0 :(得分:0)

您可以使用System.DirectoryServices在IIS6上获取证书哈希:

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

其余部分与IIS7相同。

希望这有帮助, Rotem Varon