title: "Cannot find the certificate and private key for decryption" for Certificate pfx pkcs#12 with private key
body: <p>I have the following .Net code (asp.net) for sign using client certificate.</p>
  
  <p>I have client certificate stored under local computer and not the current user.</p>
  
  <p>The client certificate  is pfx pkcs#12 and has private key</p>
  
  <p>Imported private key are NOT marked as exportable.</p>
  
  <p>my private key in client certificate protected  by password.</p>
  
  <p>On the last line above, I get the error "Cannot find the certificate
  and private key for decryption ". </p>
  
  <p>It looks like the Private Key is not accessible when using my code.</p>
  
  <p>Is there anyway for me to associate the private key to my client certificate ? Any suggestions ?</p>
  
  <pre><code>public void FirmarConCertificado(string nombreCertificado, X509Certificate2 certificate) 
  { 
      try 
      { 
   var mensaje = "Datos de prueba"; 
                  System.Text.Encoding enc = System.Text.Encoding.Default; 
                  byte[] data = enc.GetBytes(mensaje); 
  
                  var contentInfo = new System.Security.Cryptography.Pkcs.ContentInfo(data); 
                  var signedCms = new System.Security.Cryptography.Pkcs.SignedCms(contentInfo, true); 
  
                  var cmsSigner = new System.Security.Cryptography.Pkcs.CmsSigner(certificate); 
  
                  //  Sign the CMS/PKCS #7 message 
                  signedCms.ComputeSignature(cmsSigner);  // &lt;&lt;&lt;&lt;&lt;&lt;&lt; FAILS HERE
  
                  //  Encode the CMS/PKCS #7 message 
                 var ret = Convert.ToBase64String(signedCms.Encode()); 
  
   Message.Text += "Firmado con Certificado " + nombreCertificado + " encontrado en " + StoreLocation.LocalMachine; 
   } 
   catch (Exception ex) 
   { 
   Message.Text = "Error al firmar con certificado: " + ex.ToString(); 
   Message.Text += "&lt;br /&gt;&lt;br /&gt;InnerException: " + ex.InnerException; 
   } 
  
  } 
  </code></pre>
  
  <p>EDIT: Perhaps problems with identity in AppPool. The user that install the certificate in LocalMachine Store must be identity of AppPool of WebSite, and are in IIS_WPG group.</p>
  
  <p>SOLUTION: Create user in domain, add it in IIS_WPG group, add it as Identity AppPool. Install certificates in Store Local Computer using that user created.</p>