I want to establish GRPC SslCredentials() using a certificate from my Windows Certificate Store under .Net 4.8. This link is quite similar but doesn't provide a working solution.
https://github.com/grpc/grpc/issues/8978
I use code similar to the following. The ExportToPEM() is a home-grown attempt to convert the certificate to PEM format, but it doesn't work. I wish the C# wrapper for GRPC would make this easy.
var cred = new SslCredentials(GetCertificate());
Channel = new Channel("127.0.0.1", BINDING_PORT, cred);
private static string GetCertificate()
{
var storex = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
storex.Open(OpenFlags.ReadOnly);
var certificatesx = storex.Certificates.Find(X509FindType.FindBySubjectName, CERTIFICATE_SUBJECTNAME, true);
if (certificatesx.Count > 0)
{
foreach (X509Certificate2 cert in certificatesx)
{
if (!string.IsNullOrWhiteSpace(cert.FriendlyName) && cert.FriendlyName == ROOT_CERTIFICATE_FRIENDLYNAME)
{
return ExportToPEM(cert.GetRSAPrivateKey());
}
}
}
...
}