我有以下 .Net 代码 (asp.net) 用于使用客户端证书进行签名。
我将客户端证书存储在本地计算机而不是当前用户下。
客户端证书是 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。使用创建的用户在存储本地计算机中安装证书。