2016-06-10 109 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