Created
September 12, 2016 19:05
-
-
Save guitarrapc/0f2cfeba09a95647085a1180cf0c24aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CertificateExpireChecker | |
{ | |
private TimeSpan TimeOut => TimeSpan.FromSeconds(5); | |
public string Url { get; } | |
public DateTime LimitDate => DateTime.Now; | |
public bool IsExists => Certificate != null; | |
public X509Certificate2 Certificate { get; private set; } | |
public string CommonName => Certificate?.SubjectName.Name.Split(',').FirstOrDefault(x => x.StartsWith("CN=")); | |
public TimeSpan? DaysLeft => ExpiredDate - LimitDate; | |
public bool IsExpired => DateTime.Now > Certificate?.NotAfter; | |
public DateTime? ExpiredDate => Certificate?.NotAfter; | |
public string Issuer => Certificate?.Issuer; | |
public string Subject => Certificate?.Subject; | |
public string FriendlyName => Certificate?.FriendlyName; | |
public string ThumbPrint => Certificate?.Thumbprint; | |
public string SignatureAlgorithm => Certificate?.SignatureAlgorithm.FriendlyName; | |
public CertificateExpireChecker(string url) | |
{ | |
Url = url; | |
} | |
public void ReadX509Certificate2() | |
{ | |
var request = WebRequest.Create(Url) as HttpWebRequest; | |
request.Timeout = (int)TimeOut.TotalMilliseconds; | |
try | |
{ | |
using (var response = request.GetResponse()) { } | |
} | |
catch (Exception) | |
{ | |
return; | |
} | |
var cert2 = new X509Certificate2(request.ServicePoint.Certificate); | |
Certificate = cert2; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment