title: How to convert BouncyCastle X509Certificate to .NET Standard X509Certificate2 while retaining the private key?
body: <p>I need to convert a generated BouncyCastle <code>X509Certificate</code> to <code>X509Certificate2</code> with the private key getting loaded in the resulting <strong>.NET Standard</strong> <code>X509Certificate2</code> object. Is that possible? </p>
  
  <p>There is an answer for a similar question <a href="https://stackoverflow.com/a/8150799/5048935">https://stackoverflow.com/a/8150799/5048935</a> , but after generating a BouncyCastle certificate and converting it to .NET <code>X509Certificate2</code> the resulting object has its <code>HasPrivateKey</code> property set to "false".</p>
  
  <h1>Context</h1>
  
  <p>I need to load a certificate and a private key (separate Base64 strings without headers and footers) to a <code>X509Certificate2</code> object to pass it on to the app from a .NET Standard 1.6 library. The problem is, <strong>there is no <code>PrivateKey</code> property in the <code>X509Certificate2</code> class in .NET Standard</strong>! So the only way for me to actually load the private key in a <code>X509Certificate2</code> object is to combine it with the certificate itself in a .pfx file and load it like that in a constructor. </p>
  
  <p>I've got suggestions to use BouncyCastle for that ( <a href="https://stackoverflow.com/a/44465965/5048935">https://stackoverflow.com/a/44465965/5048935</a> ), so I first generate a BouncyCastle <code>X509Certificate</code> from the Base64 strings and then try to convert it to <code>X509Certificate2</code> while retaining the private key.</p>