繁体   English   中英

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

[英]How to remove certificate from Store cleanly

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

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

谢谢

您可以尝试使用.Net Framework中的X509Store和相关类来从证书存储中删除证书。 以下代码示例从当前用户的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();

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

  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跟随下面的链接帖子,它工作得很好...使用管理控制台。

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

资料来源: 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

UPDATE

咄! 我简直不敢相信我没试过! 您可以使用以下内容删除证书:

Get-ChildItem Cert:\CurrentUser\My | Where-Object {$_.Subject -eq 'CN=certCN'} | Remove-Item

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM