繁体   English   中英

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

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

I am trying to export all the SSL certificates on IIS 6.0 sites from a specificed remote server to a centralized backup server so we can migrate and/or backup our SSL certificates, however I cannot figure out how to do this with IIS 6.0 (all our暂存和生产中的服务器仍运行 IIS 6.0)。 有没有办法使用 C# 和 System.Management 来定位 IIS 6.0 web 站点。 我已经尝试了我能想到的一切。

Pseduo Logic: Get a list of all IIS Web Sites on Server X If the site has an SSL certificate binding associated with it, export the SSL certificate with the name of the IIS Web Site.

这是更接近 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)
                {

                }
            }

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

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

rest 与 IIS7 中的相同。

希望这会有所帮助,Rotem Varon

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM