For those that find this, and are using dotnet 5+
var pem = File.ReadAllText("path/to/my.pem");
var key = X509Certificate2.CreateFromPem(pem);
If you need the private + public,
var certPem = File.ReadAllText("path/to/fullchain.pem");
var keyPem = File.ReadAllText("path/to/privkey.pem");
var x509 = X509Certificate2.CreateFromPem(certPem, keyPem);
This can then be hooked into Kestrel like this:
webApplicationBuilder.WebHost.ConfigureKestrel(s => {
s.ListenAnyIP(443, options => {
options.UseHttps(x509);
}
And maybe also look at https://www.scottbrady91.com/c-sharp/pem-loading-in-dotnet-core-and-dotnet