2010-10-18 530 views
3

我有以下.Net代码(asp.net)用于使用客户端证书进行签名。“无法找到解密的证书和私钥”证书pfx pkcs#12私钥

我有客户端证书存储在本地计算机下,而不是当前用户。

客户端证书是PFX PKCS#12和具有私钥

导入的私钥未标记为可导出。

我的私钥在客户端证书中受密码保护。

在上面的最后一行,我得到错误“找不到证书 和私钥解密”。

它看起来像使用我的代码时私人密钥不可访问。

有没有办法让我把私钥和我的客户端证书关联起来?有什么建议么 ?

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; 
} 

} 

编辑:也许问题与AppPool身份。在LocalMachine Store中安装证书的用户必须是WebSite的AppPool的身份,并且位于IIS_WPG组中。

解决方案:在域中创建用户,将其添加到IIS_WPG组中,将其添加为Identity AppPool。使用创建的用户在商店本地计算机中安装证书。

+0

也许与AppPool中的身份有关的问题。在LocalMachine Store中安装证书的用户必须是WebSite的AppPool的身份,并且位于IIS_WPG组中。 – Kiquenet 2010-10-26 16:18:09

回答

2

解决方案:在域中创建用户,将其添加到IIS_WPG组中,将其添加为Identity AppPool。使用创建的用户在商店本地计算机中安装证书。