1

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

    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 函数后它不会被删除。我究竟做错了什么?

4

2 回答 2

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();
}
于 2013-10-04T12:17:19.483 回答
0

问题是这条线

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

正确的是:

store.Remove(toRemove[0]);
于 2013-10-04T12:32:39.480 回答