DEV Community

Cover image for Loading Certificates on .NET 9
Andreas Nägeli
Andreas Nägeli

Posted on

Loading Certificates on .NET 9

I've seen a lot of traffic regarding the X509CertificateLoader on my website lately, so I guess people are struggling with fixing the deprecations on the constructor of X509Certificate2 (as I did, too).

So basically:

// from file
var cert = new X509Certificate2("./myfile.pfx");

// from byte array
var stream = new MemoryStream();
var cert = new X509Certificate2(stream.ToArray());
Enter fullscreen mode Exit fullscreen mode

has become:

// from file
var cert = X509CertificateLoader.LoadCertificateFromFile("./myfile.pfx");

// from byte array
var stream = new MemoryStream();
var cert = X509CertificateLoader.LoadPkcs12(mem.ToArray(), null);
Enter fullscreen mode Exit fullscreen mode

There are of course more overloads, but you will get the idea.

Cover picture by Marjan Blan

Top comments (0)