【问题标题】:How to get a list of sites and SSL certificates from IIS 6.0 using C#, WMI, and/or System.Management?如何使用 C#、WMI 和/或 System.Management 从 IIS 6.0 获取站点列表和 SSL 证书?
【发布时间】:2011-10-03 04:18:15
【问题描述】:

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

伪逻辑: 获取服务器 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)
                {

                }
            }

【问题讨论】:

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


    【解决方案1】:

    您可以使用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

    【讨论】:

      猜你喜欢
      • 2019-06-15
      • 1970-01-01
      • 1970-01-01
      • 2011-02-22
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多