如何干净地从Store中删除证书

dae*_*aai 14 c# vb.net powershell wizard x509certificate

您可以使用certmgr.msc中的向导(右键单击安装)将证书安装到证书库中吗?有谁知道如何通过使用向导/代码(pref.)/脚本"干净地"删除所有证书?

我希望能够从LocalMachine和/或CurrentUser商店中删除所有(我之前安装的),而不会留下任何残留物.

谢谢

Han*_*ans 14

您可以尝试使用X509Store.Net Framework中的相关类来从证书存储中删除证书.以下代码示例从当前用户的My store中删除证书:

// 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();
Run Code Online (Sandbox Code Playgroud)

开始编辑: 根据评论部分中的评论,我用代码示例更新了我的答案,该代码示例显示了如何删除证书和链中的所有证书:

  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);
Run Code Online (Sandbox Code Playgroud)

结束编辑

希望这可以帮助.

  • 它会确保从机器中删除所有证书,包括链中的证书吗? (2认同)

D. *_*bya 5

老线程,但我只是使用 Win 7 按照下面的链接帖子操作,效果很好......使用管理控制台。

  1. 开始 -> 运行 -> mmc.exe
  2. 单击文件->“添加/删除管理单元”
  3. 选择证书,点击添加
  4. 选择“计算机帐户”,单击“下一步”。
  5. 选择“本地计算机”,点击完成
  6. 单击“确定”,这将带您返回 MMC
  7. 在左窗格中,展开证书(本地计算机)
  8. 用列出的证书做你想做的事......

来源: http ://windowssecrets.com/top-story/certificate-cleanup-for-most-personal-computers/

  • 虽然此链接可以回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会变得无效。 (2认同)