Code to check SSL Client Certificate Expiration remotely
Reference: System.Net.ServicePointManager, another sample code from stackOverflow
Please feel free to modify the code to fit your needs.
Language: C# (C Sharp) using MS Visual Studio Express 2010
Reference: System.Net.ServicePointManager, another sample code from stackOverflow
Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Diagnostics; namespace CertCheck { class Program { static void Main(string[] args) { //Get URL from command line to check if (args.Length==0) { Console.WriteLine("Usage: CertCheck <https://url> [-v]"); Console.WriteLine("Enter an url with https prefix"); Console.WriteLine("Add -v for verbose output. By default, this will print just the number of days on the console"); return; } string https_url = args[0]; bool bVerbose=false; if (args.Length > 1) if (args[1] == "-v") bVerbose = true; HttpWebRequest http_req = null; //Define the Certificate Verification Callback method ServicePointManager.ServerCertificateValidationCallback = (state,cert,certChain,sslerr)=> { DateTime certExpirationDate= DateTime.Parse(cert.GetExpirationDateString()); if (bVerbose) { Console.WriteLine("Certificate Expires in {0} days on {1}", certExpirationDate.Subtract(DateTime.Now).Days,certExpirationDate.ToShortDateString()); Console.WriteLine("Certificate Serial Number: {0}",cert.GetSerialNumberString()); Console.WriteLine("Certificate Issued to: {0}",cert.Subject); } else Console.WriteLine(certExpirationDate.Subtract(DateTime.Now).Days); return true; }; //Connect to the URL to validate the certificate try { http_req=(HttpWebRequest)HttpWebRequest.Create(https_url); HttpWebResponse http_resp = (HttpWebResponse)http_req.GetResponse(); http_resp.Close(); } catch (Exception http_excep) { Debug.WriteLine("Error Checking site {0} : {1}",https_url,http_excep.ToString()); } } } } |
Please feel free to modify the code to fit your needs.