2016-06-10 92 views
0

我想導入PFX certificate使用certutil.exe。當我使用這個過程:導入無密碼PFX引發異常

Process.Start(
       new ProcessStartInfo() 
       { 
        CreateNoWindow = true, 
        WindowStyle = ProcessWindowStyle.Hidden, 
        FileName = "certutil", 
        Arguments = string.Format("-f -p {0} -importPFX \"{1}\"", passwordPFX, _pathServerCerPFX) 
       } 
      ).WaitForExit(); 

它工作正常的一切。 (在這種情況下,證書是使用密碼創建的)。但是,如果我沒有密碼創建證書,並嘗試導入這樣的:

Process.Start(
       new ProcessStartInfo() 
       { 
        CreateNoWindow = true, 
        WindowStyle = ProcessWindowStyle.Hidden, 
        FileName = "certutil", 
        Arguments = string.Format("-f -p -importPFX \"{0}\"", _pathServerCerPFX) 
       } 
      ).WaitForExit(); 

然後我得到一個錯誤,指出:A specified logon session does not exist. It may already have been terminted. 什麼可能我會丟失?

回答

0

在您的Arguments = string.Format("-f -p -importPFX \"{0}\"", _pathServerCerPFX)代碼行中,您使用的是-p,它基本上代表password參數。刪除該部分代碼可以解決您的問題。您的代碼應該如下所示:

Process.Start(
       new ProcessStartInfo() 
       { 
        CreateNoWindow = true, 
        WindowStyle = ProcessWindowStyle.Hidden, 
        FileName = "certutil", 
        Arguments = string.Format("-f -importPFX \"{0}\"", _pathServerCerPFX) 
       } 
      ).WaitForExit(); 

希望這可以解決您的問題。

+0

雖然轉義-p,我仍然得到提示等待我輸入密碼。你如何在代碼中繞過這個提示? – qingjinlyc