title: m_safeCertContext is an invalid handle
body: <p>I've been wrestling with a problem, maybe you guys can point me in the right direction.</p>
  
  <p>I'm trying to digitally sign a pdf, on the webserver, over an https connection.</p>
  
  <p>At page load i'm doing as so:</p>
  
  <pre><code>HttpClientCertificate cs = Request.ClientCertificate;
  X509Certificate card = new X509Certificate(cs.Certificate);
  Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser();
   Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(card.GetRawCertData())};
  </code></pre>
  
  <p>I'm getting the error "m_safeCertContext is an invalid handle" at that last line of code.</p>
  
  <p>Please note that:</p>
  
  <ul>
  <li>I am getting the same error using 2 completely different certificates.</li>
  <li>The certificate is being retrieved to the "card" variable ok.</li>
  <li>I used to get the card to X509Certificate2 but i read yesterday somewhere I'm not being able to find that the error could be solved by casting as a X509Certificate and then downcasting to X509Certificate2. It was one of those "well... this does not makes any sense but i havent tried it yet" moments.</li>
  <li>I have tried to add <code>[System.Security.SecurityCritical, System.Security.SecurityTreatAsSafe]</code> property to all methods and even the class to see if it would work... no such luck. </li>
  </ul>
  
  <p>Can anyone one give me a hint?</p>