从存储中删除证书不起作用
本文关键字:证书 不起作用 删除 存储 | 更新日期: 2023-09-27 18:34:05
我正在尝试从存储中删除证书并使用此功能:
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 函数后不会删除它。我做错了什么?
需要打开存储以进行读写。
试试这个:
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();
}
问题是这一行
store.Certificates.Remove(toRemove[0]);
正确的是:
store.Remove(toRemove[0]);