gpt4 book ai didi

c# - 从远程服务器读取证书哈希

转载 作者:可可西里 更新时间:2023-11-01 12:01:23 27 4
gpt4 key购买 nike

尝试使用以下代码从远程计算机获取证书哈希时收到错误消息:

private string getCertHash(string Hostname)
{
string result = "";

using (ServerManager serverManager = ServerManager.OpenRemote(Hostname))
{
SiteCollection siteCollection = serverManager.Sites;
foreach (Site site in siteCollection)
{
foreach (Binding binding in site.Bindings)
{
if (binding.Protocol == "https")
{
// Error here
foreach (Byte certhashbyte in binding.CertificateHash)
result += certhashbyte.ToString();
}
}
}
}

return result;
}

我收到的错误更详细:

'binding.certhashbyte' threw an exception of type 'System.Runtime.InteropServices.COMException'

Either the application has not called WSAStartup, or WSAStartup failed. (Exception from HRESULT: 0x8007276D)

如果我替换以下行:

using (ServerManager serverManager = ServerManager.OpenRemote(Hostname))

using (ServerManager serverManager = new ServerManager)

使用本地服务器工作正常。

请注意,使用 ServerManager.OpenRemote(Hostname) 通常有效(我得到了所有其他信息,只有 CertHash 信息导致错误。我在两台机器(本地和远程)上都有管理员权限。系统是 Windows 2008 R2使用 IIS 7.5。

请告诉我我做错了什么/缺少了什么/为什么会发生错误。

最佳答案

出于某种原因,上面的代码现在可以正常工作了。我找不到为什么它不起作用,但我确实找到了一些我想分享的替代方案:

  1. 使用 this code这是一个示例用法:

    CertStoreReader certStoreReader = new CertStoreReader();
    foreach (X509Certificate2 x509 in certStoreReader.GetCertificates(credentials.Hostname))
    {
    certHash = x509.GetCertHashString();
    }
  2. 使用上面第一次尝试的 ServerManager(在问题中)

    using (ServerManager serverManager = ServerManager.OpenRemote(server))
    {
    Site site = serverManager.Sites.Where(s => s.Name == websiteName).Single();
    foreach (Binding binding in site.Bindings)
    {
    certHash += BitConverter.ToString(binding.CertificateHash).Replace("-", string.Empty);
    }
    }
  3. 使用 DirectoryEntry

    DirectoryEntry dir = new DirectoryEntry(@"IIS://" + server + @"/W3SVC/" + siteID);
    PropertyValueCollection vals = dir.Properties["SSLCertHash"];
    foreach (string entry in vals)
    {
    certHash += entry;
    }
  4. 使用 WMI。我在 WebAdministration 命名空间中使用了这个查询:

    "SELECT CertificateHash FROM SSLBinding WHERE IPAddress = '" + IP + "' AND Port = " + port

关于c# - 从远程服务器读取证书哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9586602/

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