2010-10-18 204 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。使用創建的用戶在商店本地計算機中安裝證書。