gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:04:58 27 4
gpt4 key购买 nike

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

伪逻辑:获取服务器 X 上所有 IIS 网站的列表如果站点具有与其关联的 SSL 证书绑定(bind),则使用 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)
{

}
}

最佳答案

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

希望对您有所帮助,罗腾瓦隆

关于c# - 如何使用 C#、WMI 和/或 System.Management 从 IIS 6.0 获取站点列表和 SSL 证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6657076/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com