首页 > 解决方案 > 从 netsh.exe 获取证书

问题描述

我们有一个使用 https 公开的 WCF 服务。证书在控制台中通过配置

netsh http add sslcert ipport=0.0.0.0:<port> certhash={<thumbprint>} appid={<some GUID>}

在启动期间,我这样做是为了检查是否为端口配置了任何证书侦听:

Process bindPortToCertificate = new Process();
bindPortToCertificate.StartInfo.FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86), "netsh.exe");
var arguments= string.Format("http show sslcert ipport=0.0.0.0:{0}", httpsPort);
bindPortToCertificate.StartInfo.Arguments = arguments;

bindPortToCertificate.StartInfo.RedirectStandardOutput = true;
bindPortToCertificate.StartInfo.RedirectStandardError = true;
bindPortToCertificate.StartInfo.UseShellExecute = false;
bindPortToCertificate.StartInfo.CreateNoWindow = true;

bindPortToCertificate.Start();
bindPortToCertificate.WaitForExit();

var result = bindPortToCertificate.StandardOutput.ReadToEnd();
var resultError = bindPortToCertificate.StandardError.ReadToEnd();

if (!string.IsNullOrWhiteSpace(result) && 
result.Contains(httpsPort.ToString()))

有了这个结果:

IP:port                      : 0.0.0.0:54322
Certificate Hash             : yyyyyyyyyyyyyyyyyyyy
Application ID               : {xxxxxxxxxxxxxxxxx}
Certificate Store Name       : (null)
Verify Client Certificate Revocation : Enabled
Verify Revocation Using Cached Client Certificate Only : Disabled
Usage Check                  : Enabled
Revocation Freshness Time    : 0
URL Retrieval Timeout        : 0
Ctl Identifier               : (null)
Ctl Store Name               : (null)
DS Mapper Usage              : Disabled
Negotiate Client Certificate : Disabled
Reject Connections           : Disabled
Disable HTTP2                : Not Set
Disable QUIC                 : Not Set
Disable TLS1.3               : Not Set
Disable OCSP Stapling        : Not Set

我想对证书进行一些检查(有效期至、撤销等),但我找不到一个好的 API 来访问绑定信息。看起来这仅在控制台中可用。我知道如何在证书存储中找到证书,但我不想使用字符串魔术从输出中提取证书哈希,因为我必须考虑使用不同语言运行的系统。

知道如何通过代码访问 Windows 机器上的 SSL 证书绑定吗?

标签: c#.nethttpscertificatewindow-server

解决方案


推荐阅读