title: Access PrivateKey in x509 certificate in windows certificate store marked non-exportable
body: <p>I've managed to access the certificates collection and target the specific certificate by thumbprint. I can access all its properties, and its "HasPrivateKey" is set to true. </p>
  
  <p>The "Exportable" in the CspKeyContainerInfo is marked as false.</p>
  
  <p>When I do a PrivateKey.ToXmlString(false) meaning I do not get the key parameters but just the Modulus and Exponent its fine:</p>
  
  <pre><code>&lt;RSAKeyValue&gt;
      &lt;Modulus&gt;4hjg1ibWXHIlH...ssmlBfMAListzrgk=&lt;/Modulus&gt;
      &lt;Exponent&gt;AQAB&lt;/Exponent&gt;
  &lt;/RSAKeyValue&gt;
  </code></pre>
  
  <p>but if I set it to toXmlString(true) to get all the values it fails:</p>
  
  <pre><code>&lt;RSAKeyValue&gt;
      &lt;Modulus&gt;4hjg1ibWXHIlH...ssmlBfMAListzrgk=&lt;/Modulus&gt;
      &lt;Exponent&gt;AQAB&lt;/Exponent&gt;
      &lt;P&gt;8QZCtrmJcr9uW7VRex+diH...jLHV5StmuBs1+vZZAQ==&lt;/P&gt;
      &lt;Q&gt;8CUvJTv...yeDszMWNCQ==&lt;/Q&gt;
      &lt;DP&gt;elh2Nv...cygE3657AQ==&lt;/DP&gt;
      &lt;DQ&gt;MBUh5XC...+PfiMfX0EQ==&lt;/DQ&gt;
      &lt;InverseQ&gt;oxvsj4WCbQ....LyjggXg==&lt;/InverseQ&gt;
      &lt;D&gt;KrhmqzAVasx...uxQ5VGZmZ6yOAE=&lt;/D&gt;
  &lt;/RSAKeyValue&gt;
  </code></pre>
  
  <p>However if I re-import the certificate and mark it "Exportable", then the above gives me access to all the KeyInformation I need to proceed.</p>
  
  <p>Is there a way to keep a key Non-Exportable, and still access the information with toXmlString(true) programatically by using x509....Certiificate2 object?</p>
  
  <pre><code>   X509Certificate2Collection xcollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByThumbprint, "4678237245FEDC8059D113675955DFB870D36BF4", false);
  
          foreach (X509Certificate2 x509 in xcollection)
          {
              var y = x509.PrivateKey.ToXmlString(false);
              var f = x509.PrivateKey.ToXmlString(true);
          }
  </code></pre>
  
  <p>Cheers</p>