Getting An error occurred while signing: Failed to sign file.exe. SignTool Error: No certificates were found that met all the given criteria

Getting An error occurred while signing: Failed to sign file.exe. SignTool Error: No certificates were found that met all the given criteria

好的 - 所以这真的很奇怪。我有一个对文件进行签名的 TFS 构建,我收到了上面的消息。如果我从构建中查看日志,它说它成功地签署了我的文件并为其添加了时间戳,(有一个 .proj 文件手动调用 signtool)但在另一个步骤下面(不确定确切位置) - 我假设它在ClickOnce 签名我得到了错误。

我可以使用与构建使用相同的参数使用 Signtool 自己对文件进行签名,所以我想也许我需要导入他的证书,所以我打开了 mmc,添加了证书管理单元,通过了Import Wizard 使用 Local Machine 来安装它(TFS 构建运行在与我不同的帐户下,我不知道该帐户的密码,所以我认为在机器级别安装它是可行的)。我浏览了该文件并将其成功导入到受信任的根证书颁发机构(见下文):

并且在构建时仍然出现错误。 signtool 是从 TFS 构建中调用的 .proj 文件调用的,但随后在 ClickOnce 期间由构建再次调用。通过 VS 屏幕导入证书后,我现在看到:

并得到这个错误:

C:\Program Files (x86)\MSBuild.0\bin\Microsoft.Common.CurrentVersion.targets (2718): Unable to find code signing certificate in the current user’s Windows certificate store. To correct this, either disable signing of the ClickOnce manifest or install the certificate into the certificate store.
C:\Program Files (x86)\MSBuild.0\bin\Microsoft.Common.CurrentVersion.targets (2718): Cannot import the following key file: . The key file may be password protected. To correct this, try to import the certificate again or import the certificate manually into the current user’s personal certificate store.
C:\Program Files (x86)\MSBuild.0\bin\Microsoft.Common.CurrentVersion.targets (2718): Importing key file "les.pfx" was canceled.

该证书与 .csproj 位于同一文件夹中,并且已导入到商店中。

Here's the cert info and the Thumbprint matches what's in the .csproj file:

有什么我可能在这里遗漏的想法吗?

根据错误信息,您必须将证书导入到代理机器的个人存储中。当您从个人商店引用证书时,它不会询问密码,因此您可以访问您的代码签名证书。

如果使用 ClickOnce 构建多个项目,则必须将证书导入每个项目。

请尝试使用 Visual Studio 命令提示符在您的构建代理机器中导入证书:

  1. 单击开始所有程序Microsoft Visual StudioVisual Studio 工具Visual Studio 命令提示符.
  2. 键入以下命令示例:

    sn -i "c:\Pathtofile\.pfx" VS_KEY_C1D3ACB8FBF1AGK4
    

注: 带 -i 参数的 sn.exe 将密钥对安装到名为的密钥容器中。

  1. 将 pfx 文件重新导入 Visual Studio。

您还可以尝试创建一个 PowerShell 脚本,并在您的构建定义中 运行 pre-build scripts 导入证书。

供您参考的PowerShell脚本示例:

$pfxpath = 'pathtoees.pfx'
$password = 'password'

Add-Type -AssemblyName System.Security
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$cert.Import($pfxpath, $password, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]"PersistKeySet")
$store = new-object system.security.cryptography.X509Certificates.X509Store -argumentlist "MY", CurrentUser
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]"ReadWrite")
$store.Add($cert)
$store.Close()

引用这些线程: