DanielG
DanielG

Reputation: 1237

c# how to read the certificate information from an untrusted server

I found several topics, about how to retrieve the certificate information from a URL, e.g. this one: https://stackoverflow.com/a/2941934/1682946.

But all those solutions only work if the certificate is trusted. Otherwise I get an exception when calling request.GetResponse(). However, I need to get the certificate issuer and expiration data in the case that it is untrusted. I want to show the user of my application an error and give him information about the provided ssl certificate.

Upvotes: 2

Views: 987

Answers (1)

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107626

Perhaps you could hook into the certificate validation callback to inspect the properties of the certificate:

request.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => { 
    // investigate certificate parameter
    X509Certificate2 x509 = new X509Certificate2(certificate);
    Console.WriteLine("Certificate expired on: {0}", x509.NotAfter);
    return true; // true to bypass, false otherwise
};
...
request.GetResponse();

Upvotes: 2

Related Questions