问题描述
我一直在努力解决问题,也许你们可以为我指出正确的方向。
我正在尝试在pdf上进行数字签名Web服务器,通过https连接。
在页面加载时,我这样做的是:
HttpClientCertificate cs = Request.ClientCertificate;
X509Certificate卡=新的X509Certificate(cs.Certificate);
Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser();
Org.BouncyCastle.X509.X509Certificate []链=新的Org.BouncyCastle.X509.X509Certificate [] {cp.ReadCertificate(card.GetRawCertData())};
在最后一行代码中出现错误 m_safeCertContext是无效的句柄。 / p>
请注意:
- 我使用2个完全不同的错误证书。
- 正在将证书检索到卡变量中。
- 我曾经将卡获取到X509Certificate2,但昨天在某处阅读我找不到通过将其转换为X509Certificate然后向下转换为X509Certificate2可以解决的错误。那是那些嗯...这没有任何意义,但我还没有尝试过的时刻之一。
- 我尝试添加
[System。所有方法,甚至是类的Security.SecurityCritical,System.Security.SecurityTreatAsSafe]
属性,以查看其是否有效...运气不好。
有人可以给我提示吗?
只要您访问密码术中的未初始化字段,就可能发生这种情况。
在您的代码中,如果 Request.ClientCertificate
返回的对象没有原始证书数据,那么您将看到错误当您在第四行调用 card.GetRawCertData()
时。
作为一个简单的测试,请尝试以下操作:
var cert = new System.Security.Cryptography.X509Certificates.X509Certificate2();
Console.WriteLine(cert.Thumbprint);
这将引发以下异常,因为没有可用的指纹:
m_safeCertContext是无效的句柄。
具有给定的堆栈跟踪:
<$ p System.Security.Cryptography.X509Certificates.X509Certificate.ThrowIfContextInvalid()
在System.Security.Cryptography.X509Certificates.X509Certificate.SetThumbprint()
在System.Security。 Cryptography.X509Certificates.X509Certificate.GetCertHashString()
在System.Security.Cryptography.X509Certificates.X509Certificate2.get_Thumbprint()
在MyEncryptionUtility.EncryptionUtilityForm.button1_Click(对象发送者:EventArgs) cryptEncryptionUtilityForm.cs:行2864
I've been wrestling with a problem, maybe you guys can point me in the right direction.
I'm trying to digitally sign a pdf, on the webserver, over an https connection.
At page load i'm doing as so:
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())};
I'm getting the error "m_safeCertContext is an invalid handle" at that last line of code.
Please note that:
- I am getting the same error using 2 completely different certificates.
- The certificate is being retrieved to the "card" variable ok.
- I used to get the card to X509Certificate2 but i read yesterday somewhere I'm not being able to find that the error could be solved by casting as a X509Certificate and then downcasting to X509Certificate2. It was one of those "well... this does not makes any sense but i havent tried it yet" moments.
- I have tried to add
[System.Security.SecurityCritical, System.Security.SecurityTreatAsSafe]
property to all methods and even the class to see if it would work... no such luck.
Can anyone one give me a hint?
This can happen any time you access uninitialized fields in cryptography.
In your code, if Request.ClientCertificate
returns an object with no raw certificate data then you will see the error when you call card.GetRawCertData()
on your fourth line.
As a simple test, try the following:
var cert = new System.Security.Cryptography.X509Certificates.X509Certificate2();
Console.WriteLine(cert.Thumbprint);
This will throw the following exception because there is no thumbprint available:
m_safeCertContext is an invalid handle.
with the given stack trace:
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
这篇关于m_safeCertContext是无效的句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!