c# - 从 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 证书绑定吗?
解决方案
推荐阅读
- python - 在 Airflow 上使用 DataprocOperator 的组件网关
- javascript - 告诉 PHP 后端浏览器已从前端 Angular 关闭
- vba - 将文件保存到目录和子目录
- python - 我无法在 linux 中导入 pygame 模块
- javascript - 如何通过 ajax 提交 django 模型表单,防止在提交时重新加载/刷新
- r - 基于条件数据的匹配
- wordpress - 单品汇总后如何在 WooCommerce 上添加 html 代码
- git - 创建 git repo 并将本地目录推送到新初始化的 Azure Repos
- c# - 如何使用 SelectTokens 查询多个字段?
- javascript - 使用“window.location.pathname”编写路由特定逻辑的好习惯吗?