DEV Community

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

Posted on

1

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)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more