确定IIS SSL证书的过期日期

7

我需要以程序的方式确定我IIS服务器上SSL证书的过期日期。最好使用C#来完成,但如果只能使用VB脚本也可以接受。

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

谢谢

4个回答

11

我不熟悉使用 VBS 和/或 WMI 进行此检查的方法,因为这可能存在安全漏洞,由于可能会暴露私钥。但是,有一种方法可以利用普通的 HTTPS 连接获取证书的公共信息。如果您使用 IE 连接到任何安全网站,然后转到“文件”菜单,查看“属性”,然后点击“证书”按钮,即可显示站点的公共证书信息对话框。您可以使用 C# 以编程方式访问此信息。

基本上,您需要打开到服务器端口 443 的 TCP 连接,然后接收 SSL 数据。在此数据流中包含了关于证书的公共信息,您可以验证它并从中提取所需的所有信息,包括过期日期。以下是一些样例代码:

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;
};

而且很酷的是,这种方法在技术上应该适用于任何Web服务器……我只在IIS 6和7上进行了测试。


8

Jonas Gorauskas的解决方案简化版:

public class SslCertificateExpirationChecker
{
    public DateTime GetCertificateExpirationDate(string host, int port)
    {
        TcpClient client = new TcpClient(host, port);

        X509Certificate2 x509 = null;
        SslStream sslStream = new SslStream(client.GetStream(), false, 
            delegate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors sslError)
            {
                x509 = new X509Certificate2(cert);
                return true;
            });
        sslStream.AuthenticateAsClient(host);
        client.Close();
        return x509.NotAfter;
    }
}

使用方法:

var expirationDate = checker.GetCertificateExpirationDate("www.mydomain.com", 443);

1

1

您可以使用Microsoft.Web.Administration来完成此操作,参见此帖子和这个博客文章。我认为您应该能够反向操作-获取正确的存储区,然后获取正确的证书。

[编辑]嗯,我现在感到困惑了。我不确定Microsoft.Web.Administration是否支持低于IIS 7的任何内容..[/编辑]


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接