-1

I am able to get ssl certificate expiry date using http web request c# for Google.com but unable to get the ssl certificate expiry date for my web application hosted in azure

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://sitename.azurewebsites.us");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();
 X509Certificate cert = request.ServicePoint.Certificate;
 X509Certificate2 cert2 = new X509Certificate2(cert);
string cedate = cert2.GetExpirationDateString();
dinesh kumar
  • 73
  • 1
  • 8

1 Answers1

0

If you want to get the ssl certificate details, you could use X509Certificate2UI to retrieve the certificate. You could try with the below code.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls |
                                       SecurityProtocolType.Tls11 |
                                       SecurityProtocolType.Tls12;
           HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://sitename.azurewebsites.net");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            response.Close();
            //retrieve the ssl cert and assign it to an X509Certificate object
            X509Certificate cert = request.ServicePoint.Certificate;

            //convert the X509Certificate to an X509Certificate2 object by passing it into the constructor
            X509Certificate2 cert2 = new X509Certificate2(cert);

            //show expiratin date
            Console.WriteLine("expiratin date: "+cert2.GetExpirationDateString());

enter image description here

dinesh kumar
  • 73
  • 1
  • 8
George Chen
  • 13,703
  • 2
  • 11
  • 26
  • @dinesh kumar , my site is a sitename.azurewebsite.net, what did you get from cert2 object or did you get any error? Or maybe you could share your site url. – George Chen Sep 06 '19 at 06:27
  • @dinesh kumar, please check this [picture](https://i.stack.imgur.com/Ur2yn.png), I could get the date. – George Chen Sep 06 '19 at 06:37
  • @dinesh kumar, i test the second url it also could be retrieved. Could your share what you get? – George Chen Sep 06 '19 at 06:41
  • Unable to read data from transport connection. An existing connection is forcibly closed by remote host. From the url HttpWebResponse response = (HttpWebResponse)request.GetResponse(); – dinesh kumar Sep 06 '19 at 06:53
  • So your problem isn't couldn't get the certificate but the host connection. – George Chen Sep 06 '19 at 07:02
  • @dinesh kumar,maybe you could refer to this answer firstly to solve your exception.https://stackoverflow.com/a/21447045/10383250 – George Chen Sep 06 '19 at 07:11
  • it is working now after adding the lines before web request, ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; – dinesh kumar Sep 06 '19 at 07:12
  • Glad you could solve it, if my answer could help you, you could accept it as the answer. – George Chen Sep 06 '19 at 07:14