Asp Mvc使用私钥提示客户端证书

时间:2016-05-17 16:29:40

标签: asp.net-mvc digital-signature x509certificate digital-certificate

我想创建一个Web应用程序,服务器在其中创建一个pdf文档,然后用户使用一些个人数字证书(eID,Id卡,nif等)对其进行签名。

问题是,当我尝试localy(Debug)时它起作用,因为我在本地机器上安装了证书,但是当我发布时,证书的私钥不在证书中,所以,我可以不签署pdf。

public ActionResult Index()
{
    HttpClientCertificate cert = Request.ClientCertificate;
    X509Certificate2 x509cert2 = new X509Certificate2(cert.Certificate);
}

在Debug中,因此,在本地机器上x509cert2.PrivateKey不为null,但如果我发布,x509cert2.PrivateKey为null,那么,当我尝试时:

        byte[] contentPdfUnsigned = System.IO.File.ReadAllBytes(path + name + ".pdf");
        ContentInfo objContent = new ContentInfo(contentPdfUnsigned);
        SignedCms objSignedData = new SignedCms(objContent);
        CmsSigner objSigner = new CmsSigner(x509cert2);
        objSignedData.ComputeSignature(objSigner, false);
        byte[] bytSigned = objSignedData.Encode();

在行“objSignedData.ComputeSignature(objSigner,false);”中引发异常:

System.Security.Cryptography.CryptographicException:Key不存在。

是否有一些方法可以让用户将私钥传递给我?

执行localy时x509cert2.PrivateKey.ToXMLString(true);

提示此消息:

enter image description here

英文:

enter image description here

有没有办法提示此消息(或其他)允许用户传递私钥?

1 个答案:

答案 0 :(得分:1)

在大多数情况下,用户无法或应该将私钥传递给您 - 这在技术上是不可能的,并且会打破私钥的想法(将其传递给您使其隐私无效)。您需要创建一个客户端模块来进行实际签名,或者使用一些预先创建的模块。 In this answer我描述了我们提供的解决方案,您也可以创建自己的客户端模块。

相关问题