簡體   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