【问题标题】:Why would I get ExecutionEngineException in OpenRead on X509CertificateStore?为什么我会在 X509CertificateStore 上的 OpenRead 中获得 ExecutionEngineException?
【发布时间】:2009-08-19 05:33:43
【问题描述】:

我正在使用带有安全令牌的 WebServicesClientProtocol 类,并使用以下代码查找要使用的安全令牌:

private static X509SecurityToken GetSecurityCertificate(string subject)
{
    X509CertificateStore localStore = X509CertificateStore.LocalMachineStore(X509CertificateStore.MyStore);
    X509SecurityToken securityToken = null;
    if (localStore.OpenRead())
    {
       X509CertificateCollection certificateCollection = localStore.FindCertificateBySubjectString(subject);
        if (certificateCollection.Count == 0) throw new Exception("Skilríki finnst ekki í skilríkjageymslu");
        securityToken = new X509SecurityToken((X509Certificate)certificateCollection[0]);
    }
    localStore.Close();
    return securityToken;
}

时不时地(但并非总是如此,而且我无法确定确切的时间)我得到以下异常:

alt text http://www1.ruedenet.is/files/exception.png

【问题讨论】:

  • 其实我也有同样的问题。您是否找到了解决方案,如果有的话,如果您能在此处发布您的答案,那就太好了
  • 抱歉 - 仍然没有解决方案。是的,如果我找到解决方案,我会发布。请也这样做。谢谢。

标签: c# certificate


【解决方案1】:

我今天遇到了这个问题,我相信我已经弄清楚是什么原因造成的。静态方法需要同步。

如果您在异常发生时将鼠标悬停在 localStore 变量上并检查 Certificates 属性,您很可能会看到“InvalidOperationException - 要访问您必须使用 Open() 或 OpenRead() 的证书。 .. 等等等等”。

发生的情况是某个线程在另一个线程完成访问之前正在关闭存储。

我通过首先创建一个静态类成员来锁定存储解决了这个问题:

private static object m_storeLock = new object();

当您访问商店时,您必须执行以下操作:

public static X509Certificate FindCertificate(string certName) {
    X509CertificateStore store = null;            
    X509Certificate cert = null;

    lock (m_storeLock) {
        try {                    
            store = X509CertificateStore.LocalMachineStore(X509CertificateStore.MyStore);
            store.OpenRead();

            X509CertificateCollection col =
            (X509CertificateCollection)store.FindCertificateBySubjectString(certName);

            if (col.Count > 0) {      
                cert = col[0];
            }
        }
        catch {
        }
        finally {
            if (store != null) {
                store.Close();
            }
        }
    }                           

    if (cert == null) {
        throw new ArgumentException("Certificate not found!");
    }

    return cert;
}

为了更加安全,你可能应该锁定局部变量“cert”,但这不是生产代码的家伙...... :D~

【讨论】:

    猜你喜欢
    • 2011-01-21
    • 2011-12-09
    • 2019-12-25
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    • 2017-04-30
    • 2019-12-16
    • 1970-01-01
    相关资源
    最近更新 更多