"Cannot find certificate and private key for decryption" for pfx pkcs # 12 certificate with private key

I have the following .Net code (asp.net) for a character using a client certificate.

I have a client certificate stored under the local computer, and not the current user.

Client certificate is pfx pkcs # 12 and has a private key

The imported private key is NOT marked as exportable.

my private key in the client certificate is password protected.

In the last line above, I get the error "I can not find the certificate and private key for decryption."

It seems that closed code is not available when using my code.

Anyway, do I need to associate the private key with my client certificate? Any suggestions?

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);  // <<<<<<< 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 += "<br /><br />InnerException: " + ex.InnerException; 
 } 

} 

EDIT: , AppPool. , LocalMachine Store, AppPool WebSite IIS_WPG.

: , IIS_WPG, Identity AppPool. " " .

+3
1

: , IIS_WPG, Identity AppPool. " " .

+2

Source: https://habr.com/ru/post/1770056/


All Articles