gpt4 book ai didi

.net - 确定 SSL 证书到期日期 IIS

转载 作者:太空宇宙 更新时间:2023-11-03 12:42:53 26 4
gpt4 key购买 nike

我需要以编程方式确定 IIS 机器上 SSL 证书的到期日期。理想情况下,我想在 C# 中执行此操作,但如果 VB 脚本也是唯一可接受的方法。

环境 => IIS 版本 6 和 7、.NET 2.0、Windows 2003 和 2008

谢谢

最佳答案

我不熟悉使用 VBS 和/或 WMI 进行此检查的方法,因为这可能是一个安全整体,因为它可能会暴露私钥。但是,有一种方法可以利用普通的 HTTPS 连接来获取有关证书的公共(public)信息。如果您使用 IE 连接到任何安全网站,您可以转到"file"菜单并查看“属性”,然后单击“证书”按钮。这显示了站点的公共(public)证书信息对话框。您可以使用 C# 以编程方式获取此信息。

基本上,您要做的就是在端口 443 上打开到服务器的 TCP 连接,然后接收 SSL 数据。此数据流中包含有关证书的公共(public)信息,您可以对其进行验证并从中提取所需的所有信息,包括到期日期。这是一些示例代码:

static void Main(string[] args) {
foreach (string servername in args) {
Console.WriteLine("\n\nFetching SSL cert for {0}\n", servername);
TcpClient client = new TcpClient(servername, 443);
SslStream sslStream = new SslStream(client.GetStream(), false, callback, null);

try {
sslStream.AuthenticateAsClient(servername);
} catch (AuthenticationException ex) {
Console.WriteLine("Exception: {0}", ex.Message);
if (ex.InnerException != null) {
Console.WriteLine("Inner exception: {0}", ex.InnerException.Message);
}
Console.WriteLine("Authentication failed - closing the connection.");
}

client.Close();
}
}

以及处理证书信息的回调代码:

static RemoteCertificateValidationCallback callback = delegate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors sslError) {
X509Certificate2 x509 = new X509Certificate2(cert);

// Print to console information contained in the certificate.
Console.WriteLine("Subject: {0}", x509.Subject);
Console.WriteLine("Issuer: {0}", x509.Issuer);
Console.WriteLine("Version: {0}", x509.Version);
Console.WriteLine("Valid Date: {0}", x509.NotBefore);
Console.WriteLine("Expiry Date: {0}", x509.NotAfter);
Console.WriteLine("Thumbprint: {0}", x509.Thumbprint);
Console.WriteLine("Serial Number: {0}", x509.SerialNumber);
Console.WriteLine("Friendly Name: {0}", x509.PublicKey.Oid.FriendlyName);
Console.WriteLine("Public Key Format: {0}", x509.PublicKey.EncodedKeyValue.Format(true));
Console.WriteLine("Raw Data Length: {0}", x509.RawData.Length);

if (sslError != SslPolicyErrors.None) {
Console.WriteLine("Certificate error: " + sslError);
}

return false;
};

很酷的是,从技术上讲,这种方法应该适用于任何网络服务器......我只在 IIS 6 和 7 上测试过。

关于.net - 确定 SSL 证书到期日期 IIS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/826175/

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