C# 判断域名证书是否过期

定义一个基础类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class DomainName
{
    public string GUID { get; set; }
 
    public string Domain { get; set; }
 
    public bool IsChecked { get; set; }
 
    public bool IsExpired { get; set; }
 
    public string Message { get; set; }
 
    public List<string> SupportedSslProtocols { get; set; } = new List<string>();
 
    public string DisplayText
    {
        get
        {
            if (IsChecked)
            {
                return this.Domain + " **** Checked.";
            }
            else
            {
                return this.Domain + " **** Unchecked.";
 
            }
 
        }
    }
}

  调用https请求方法

复制代码
 1  private bool SendHttpRequest(string url)
 2  {
 3      bool result = false;
 4 
 5      var httpclientHandler = new HttpClientHandler();
 6 
 7      httpclientHandler.ServerCertificateCustomValidationCallback = (senderx, cert, chain, sslPolicyErrors)
 8          => ValidateCertificate(senderx, cert, chain, sslPolicyErrors);
 9 
10      string domainUrl = url;
11 
12      using (var httpClient = new HttpClient(httpclientHandler))
13      {
14          try
15          {
16              var response = httpClient.GetAsync(domainUrl).Result;
17 
18              result = true;
19          }
20          catch (Exception ex)
21          {
22              result = false;
23          }
24      }
25 
26      return result;
27  }
复制代码

检查证书核心代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
private DomainName domainNameEntity = new DomainName();
 
private bool ValidateCertificate(HttpRequestMessage senderx, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    bool result = false;
    domainNameEntity.IsChecked = true;
 
    var expirationDate = DateTime.Parse(certificate.GetExpirationDateString(), CultureInfo.InvariantCulture);
    var effectiveDate = DateTime.Parse(certificate.GetEffectiveDateString(), CultureInfo.InvariantCulture);
 
    if (certificate == null)
    {
        result = true;
        domainNameEntity.IsExpired = false;
        domainNameEntity.Message = "The Cert is valid.EffectiveDate : from " + effectiveDate + "  to " + expirationDate;
    }
    else
    {
        // 已过期
        if (expirationDate <= DateTime.Today)
        {
            domainNameEntity.IsExpired = true;
            domainNameEntity.Message = "The Cert has expired.! EffectiveDate : from " + effectiveDate + "  to " + expirationDate;
        }
        //还有十五天过期,则给出提示
        else if (expirationDate - DateTime.Today < TimeSpan.FromDays(15))
        {
            domainNameEntity.IsExpired = false;
            domainNameEntity.Message = "It's time to renew the certificate! EffectiveDate : from " + effectiveDate + "  to " + expirationDate;
        }
 
        //无错误
        if (sslPolicyErrors == SslPolicyErrors.None)
        {
            domainNameEntity.IsExpired = false;
            domainNameEntity.Message = "The Cert is valid. EffectiveDate : from " + effectiveDate + "  to " + expirationDate;
 
            return true;
        }
        else
        {
            domainNameEntity.IsExpired = false;
            domainNameEntity.Message = $"Cert errors: {sslPolicyErrors.ToString()}";
        }
    }
 
    return result;
}

  

 

posted @   M&Fx  阅读(40)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示