How To Extract a Certificate From a HTTPS Call

httpscertificate

Export a Server Certificate Using .NET Core

Sometimes you want to programmatically extract / export the server certificates in a HTTPS call.
Well, it’s pretty simple. See below a C# working example for .NET Core

Declare your Http Handler

private readonly HttpClientHandler handler; 

Initialize the handler, Callback method, and Certificate Export

handler = new HttpClientHandler
{
    ServerCertificateCustomValidationCallback = (message, certificate, chain, sslPolicyErrors) =>
    {
        File.WriteAllBytes("C://Temp/Test.cer", certificate.Export(X509ContentType.Cert));

        return true;
    }
};

Do a HTTP call using the handler

public string DoACall(string url)
{
    using (var client = new HttpClient(handler))
    {
        var response = client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
        return response.Result.ToString();
    }
}

Usage

var caller = new WebCaller();
var result = caller.DoACall("https://google.com/");

Source Code

https://github.com/eitanbehar/WebClient