【发布时间】:2011-10-03 08:41:22
【问题描述】:
您可以使用 certmgr.msc 中的向导将证书安装到证书存储中(右键单击安装)吗?有谁知道如何通过使用向导/代码(首选)/脚本“干净地”删除所有证书?
我希望能够从 LocalMachine 和/或 CurrentUser 存储中删除所有内容(我之前安装的)而不留下任何残留物。
谢谢
【问题讨论】:
标签: c# vb.net powershell x509certificate wizard
您可以使用 certmgr.msc 中的向导将证书安装到证书存储中(右键单击安装)吗?有谁知道如何通过使用向导/代码(首选)/脚本“干净地”删除所有证书?
我希望能够从 LocalMachine 和/或 CurrentUser 存储中删除所有内容(我之前安装的)而不留下任何残留物。
谢谢
【问题讨论】:
标签: c# vb.net powershell x509certificate wizard
您可以尝试使用 .Net Framework 中的 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();
开始编辑: 根据评论部分中的 cmets,我使用代码示例更新了我的答案,显示了如何删除证书和链中的所有证书:
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);
结束编辑
希望,这会有所帮助。
【讨论】:
旧线程,但我只是使用 Win 7 遵循下面链接的帖子,它运行良好...使用管理控制台。
来源: http://windowssecrets.com/top-story/certificate-cleanup-for-most-personal-computers/
【讨论】:
您可以尝试 certmgr.exe。以下命令从本地用户 personal\certificates 存储中删除 cn 为“commoncertname”的证书。
.\certmgr.exe -del -n commoncertname -c -s -r currentuser my
您可以在此处找到有关 certmgr.exe 的更多信息:http://msdn.microsoft.com/en-us/library/windows/desktop/aa376553%28v=vs.85%29.aspx
更新
呃!我不敢相信我没有尝试过这个!您可以使用以下内容删除证书:
Get-ChildItem Cert:\CurrentUser\My | Where-Object {$_.Subject -eq 'CN=certCN'} | Remove-Item
【讨论】: