我一直在努力解决问题,也许你们可以为我指明正确的方向。
我正在尝试通过https连接在网络服务器上对pdf进行数字签名。
在页面加载时,我这样做:
HttpClientCertificate cs = Request.ClientCertificate;
X509Certificate card = new X509Certificate(cs.Certificate);
Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser();
Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(card.GetRawCertData())};
我在最后一行代码中收到错误“m_safeCertContext是无效的句柄”。
请注意:
[System.Security.SecurityCritical, System.Security.SecurityTreatAsSafe]
属性添加到所有方法,甚至是类,以查看它是否可以工作……没有这种运气。 谁能给我一个提示?
最佳答案
每当您访问密码术中的未初始化字段时,都可能发生这种情况。
在您的代码中,如果Request.ClientCertificate
返回的对象没有原始证书数据,则在第四行调用card.GetRawCertData()
时将看到错误。
作为一个简单的测试,请尝试以下操作:
var cert = new System.Security.Cryptography.X509Certificates.X509Certificate2();
Console.WriteLine(cert.Thumbprint);
因为没有可用的指纹,这将引发以下异常:
m_safeCertContext is an invalid handle.
与给定的堆栈跟踪:
at System.Security.Cryptography.X509Certificates.X509Certificate.ThrowIfContextInvalid()
at System.Security.Cryptography.X509Certificates.X509Certificate.SetThumbprint()
at System.Security.Cryptography.X509Certificates.X509Certificate.GetCertHashString()
at System.Security.Cryptography.X509Certificates.X509Certificate2.get_Thumbprint()
at MyEncryptionUtility.EncryptionUtilityForm.button1_Click(Object sender, EventArgs e) in C:\MyEncryptionUtility\EncryptionUtilityForm.cs:line 2864
关于c# - m_safeCertContext是无效的句柄,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/442658/