您可以使用 certmgr.msc 中的向导将证书安装到证书存储中(右键单击安装)?有谁知道如何使用向导/代码(首选)/脚本“干净地”删除所有证书?
我希望能够从 LocalMachine 和/或 CurrentUser 存储中删除所有内容(我之前安装的),而不留下任何残留物。
Thanks
你可以尝试X509Store
以及 .Net Framework 中的相关类,用于从证书存储中删除证书。以下代码示例从当前用户的“我的”存储中删除证书:
// Use other store locations if your certificate is not in the current user store.
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite | OpenFlags.IncludeArchived);
// You could also use a more specific find type such as X509FindType.FindByThumbprint
X509Certificate2Collection col = store.Certificates.Find(X509FindType.FindBySubjectName, "yoursubjectname", false);
foreach (var cert in col)
{
Console.Out.WriteLine(cert.SubjectName.Name);
// Remove the certificate
store.Remove(cert);
}
store.Close();
开始编辑:根据评论部分中的评论,我用代码示例更新了我的答案,显示如何删除证书以及链中的所有证书:
X509Certificate2Collection col = store.Certificates.Find(X509FindType.FindBySubjectName, "yoursubjectname", false);
X509Chain ch = new X509Chain();
ch.Build(col[0]);
X509Certificate2Collection allCertsInChain = new X509Certificate2Collection();
foreach (X509ChainElement el in ch.ChainElements)
{
allCertsInChain.Add(el.Certificate);
}
store.RemoveRange(allCertsInChain);
END EDIT
希望这可以帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)