在.NET Core中检查证书的到期日期

在 NUnit 测试中,我需要检查证书的有效期。 下面的代码片段可用于使用自定义证书验证回调检查任何证书属性。 所有你需要做的就是在回调中读取你感兴趣的属性,这样你就可以在之后检查它们。

DateTime notAfter = DateTime.UtcNow;

var httpClientHandler = new HttpClientHandler
{
    ServerCertificateCustomValidationCallback = (request, cert, chain, policyErrors) =>
    {
        notAfter = cert.NotAfter;
        return true;
    }
};

using HttpClient httpClient = new HttpClient(httpClientHandler);
await httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, url));
            
Assert.IsTrue(notAfter > DateTime.UtcNow.AddDays(60));

这段代码只依赖于:

using NUnit.Framework;
using System;
using System.Net.Http;
using System.Threading.Tasks;
原文地址:https://www.cnblogs.com/bisslot/p/12678730.html