从存储中删除证书不起作用

时间:2013-10-04 12:10:06

标签: c# x509certificate

我正在尝试从存储中删除证书并使用此功能:

    public static void RemoveFromStorage(StoreName storeName, IEnumerable<CertInfo> certificates)
    {
        var store = new X509Store(storeName, StoreLocation.CurrentUser);
        store.Open(OpenFlags.MaxAllowed | OpenFlags.IncludeArchived);

        foreach (var cert in certificates)
        {
            var toRemove = store.Certificates.Find(X509FindType.FindByThumbprint, cert.Thumbprint, false);
            store.Certificates.Remove(toRemove[0]);
        }

        store.Close();
    }

函数不抛出任何异常,Find函数从存储中返回正确的证书,但在调用Remove函数后,它不会被删除。 我做错了什么?

2 个答案:

答案 0 :(得分:0)

需要为ReadWrite打开商店。

试试这个:

public static void RemoveFromStorage(StoreName storeName, IEnumerable<CertInfo>    certificates)
{
    var store = new X509Store(storeName, StoreLocation.CurrentUser);
    store.Open(OpenFlags.MaxAllowed | OpenFlags.IncludeArchived | OpenFlags.ReadWrite);

    foreach (var cert in certificates)
    {
        var toRemove = store.Certificates.Find(X509FindType.FindByThumbprint, cert.Thumbprint, false);
        store.Certificates.Remove(toRemove[0]);
    }

    store.Close();
}

答案 1 :(得分:0)

问题在于这一行

store.Certificates.Remove(toRemove[0]);

正确的是:

store.Remove(toRemove[0]);