如何从存储中干净地删除证书

发布于 2024-12-07 18:03:39 字数 165 浏览 1 评论 0原文

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

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

谢谢

You can install certificate into certificate store using Wizard in certmgr.msc (Right click install)? Does anyone knows how to "cleanly" remove all the certificate by either using wizard/Code (pref.) /Script ?

I want to be able to remove everything (that I have installed earlier) from the LocalMachine and/or CurrentUser Store without leaving any residue.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

热情消退 2024-12-14 18:03:39

您可以尝试使用 .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();

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

  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

希望,这会有所帮助。

You could try the X509Store and releated classes in the .Net Framework to delete a certificate from the certificate store. The following code example deletes a certificate from the current user's 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();

BEGIN EDIT:
Based on the comments in the comment section I've updated my answer with a code sample showing how to remove a certificate and all certificates in the chain:

  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

Hope, this helps.

土豪我们做朋友吧 2024-12-14 18:03:39

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

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

来源:
http://windowssecrets.com/top-story/certificate-大多数个人计算机的清理/

Old thread, but I just followed the linked post below using Win 7 and it worked nicely... Uses the Management Console.

  1. Start -> Run -> mmc.exe
  2. Click File -> "Add/Remove Snap-in"
  3. Select Certificates, click Add
  4. Select "Computer account", click Next.
  5. Select "Local computer", click Finish
  6. Click OK, which should bring you back to the MMC
  7. In left pane, expand Certificates (Local Computer)
  8. Do what you will with the listed certificates...

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

一瞬间的火花 2024-12-14 18:03:39

您可以尝试 certmgr.exe。以下命令从本地用户个人\证书存储中删除 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

You can try certmgr.exe. The following command removes a certificate with a cn of 'commoncertname ' from the local user personal\certificates store.

.\certmgr.exe -del -n commoncertname -c -s -r currentuser my

You can find more information about certmgr.exe here: http://msdn.microsoft.com/en-us/library/windows/desktop/aa376553%28v=vs.85%29.aspx

UPDATE

Duh! I can't believe I didn't try this! You can remove certificates with the following:

Get-ChildItem Cert:\CurrentUser\My | Where-Object {$_.Subject -eq 'CN=certCN'} | Remove-Item
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文