2012-10-18 30 views
0

我試圖將證書部署到Windows-my store中,以便在IIS6.0中將其用於SSL。 當應用程序運行時我沒有看到SSLDiag輸出任何錯誤,一切完美的作品,但一旦我關閉應用程序SSLDiag顯示錯誤:當應用程序關閉時,證書已從機器密鑰集中刪除

#WARNING: You have a private key that corresponds to this certificate 
but CryptAcquireCertificatePrivateKey failed 

我檢查了機器的密鑰集,發現後我關閉我的應用程序使用創建的鍵刪除文件。這裏是我檢查的位置:

C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys 

如何在機器密鑰集中保留創建密鑰文件?

這裏是我使用的代碼:我試圖X509KeyStorageFlags的不同變化

 using(var openFileDialog = new OpenFileDialog()) 
     { 
      if (openFileDialog.ShowDialog() == DialogResult.OK) 
      { 
       var password = "password"; 
       var certificate = new X509Certificate2(openFileDialog.FileName, password, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable); 
       var keystore = new X509Store(StoreName.My, StoreLocation.LocalMachine); 
       keystore.Open(OpenFlags.MaxAllowed); 
       keystore.Add(certificate); 
       keystore.Close(); 
      } 
     } 

,但文件仍然從機器取出鍵集一旦我關閉應用程序。爲什麼文件被刪除,我該如何防止它?

回答

0

我找到了解決辦法:

certificate.PrivateKey.PersistKeyInCsp = true; 

這讓私鑰留在機按鍵。 我猜想當X509Certificate2對象釋放其資源時,鍵被刪除。

相關問題