简体   繁体   English

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

[英]How to remove certificate from Store cleanly

You can install certificate into certificate store using Wizard in certmgr.msc (Right click install)? 您可以使用certmgr.msc中的向导(右键单击安装)将证书安装到证书库中吗? Does anyone knows how to "cleanly" remove all the certificate by either using wizard/Code (pref.) /Script ? 有谁知道如何通过使用向导/代码(pref。)/脚本“干净地”删除所有证书?

I want to be able to remove everything (that I have installed earlier) from the LocalMachine and/or CurrentUser Store without leaving any residue. 我希望能够从LocalMachine和/或CurrentUser商店中删除所有(我之前安装的),而不会留下任何残留物。

Thanks 谢谢

You could try the X509Store and releated classes in the .Net Framework to delete a certificate from the certificate store. 您可以尝试使用.Net Framework中的X509Store和相关类来从证书存储中删除证书。 The following code example deletes a certificate from the current user's My store: 以下代码示例从当前用户的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. 希望这可以帮助。

Old thread, but I just followed the linked post below using Win 7 and it worked nicely... Uses the Management Console. 旧线程,但我只是使用Win 7跟随下面的链接帖子,它工作得很好...使用管理控制台。

  1. Start -> Run -> mmc.exe 开始 - >运行 - > 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 单击“确定”,它将返回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/ 资料来源: http//windowssecrets.com/top-story/certificate-cleanup-for-most-personal-computers/

You can try certmgr.exe. 您可以尝试certmgr.exe。 The following command removes a certificate with a cn of 'commoncertname ' from the local user personal\\certificates store. 以下命令从本地用户personal \\ certificates存储中删除cn为'commoncertname'的证书。

.\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 您可以在此处找到有关certmgr.exe的更多信息: http//msdn.microsoft.com/en-us/library/windows/desktop/aa376553%28v=vs.85%29.aspx

UPDATE 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

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

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