您可以使用certmgr.msc中的向导将证书安装到证书存储中(右键安装)?有没有人知道如何通过向导/代码(首选)/脚本“干净地”删除所有证书?
我希望能够从LocalMachine和/或CurrentUser存储中删除我之前安装的所有内容,而不留下任何残留物。
谢谢。
您可以使用certmgr.msc中的向导将证书安装到证书存储中(右键安装)?有没有人知道如何通过向导/代码(首选)/脚本“干净地”删除所有证书?
我希望能够从LocalMachine和/或CurrentUser存储中删除我之前安装的所有内容,而不留下任何残留物。
谢谢。
X509Store
和相关类来从证书存储中删除证书。以下代码示例从当前用户的“我的”存储中删除证书:// 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);
编辑完毕
希望这有所帮助。
虽然这篇文章比较旧,但我刚刚按照下面链接的帖子在Windows 7上尝试,并且成功了...使用管理控制台。
来源: http://windowssecrets.com/top-story/certificate-cleanup-for-most-personal-computers/
.\certmgr.exe -del -n commoncertname -c -s -r currentuser my
Get-ChildItem Cert:\CurrentUser\My | Where-Object {$_.Subject -eq 'CN=certCN'} | Remove-Item