无法导入密钥文件“blah.pfx” - 错误“密钥文件可能受密码保护”
我们刚刚将 Visual Studio 2008 项目升级到 Visual Studio 2010。我们的所有程序集均使用 Verisign 代码签名证书。自升级以来,我们不断收到以下错误:
无法导入以下密钥文件:companyname.pfx。密钥文件可能受密码保护。要更正此问题,请尝试再次导入证书或使用以下密钥容器名称手动将证书安装到强名称 CSP:VS_KEY_3E185446540E7F7A
这种情况会发生在某些开发人员计算机上,而不会发生在其他计算机上。用于修复此问题的一些方法有时有效,包括:
- 从 Windows 资源管理器重新安装密钥文件(右键单击 PFX 文件并单击“安装”)
- 首次在新计算机上安装 Visual Studio 2010 会提示您输入第一次打开项目时输入密码,然后就可以了。在从 Visual Studio 2008 升级的计算机上,您不会获得此选项。
我尝试使用 SN.EXE 实用程序(强名称工具)正如错误消息所示,使用强名称 CSP 注册密钥,但每当我使用 Visual Studio 2010 附带的版本使用任何选项运行该工具时,SN.EXE 仅列出其命令行参数,而不执行任何操作。无论我提供什么论据,这种情况都会发生。
为什么会发生这种情况?解决该问题的明确步骤是什么?我即将放弃 ClickOnce 安装和 Microsoft 代码签名。
We just upgraded our Visual Studio 2008 projects to Visual Studio 2010. All of our assemblies were strong signed using a Verisign code signing certificate. Since the upgrade we continuously get the following error:
Cannot import the following key file: companyname.pfx. The key file may be password protected. To correct this, try to import the certificate again or manually install the certificate to the Strong Name CSP with the following key container name: VS_KEY_3E185446540E7F7A
This happens on some developer machines and not others. Some methods used to fix this that worked some of the time include:
- Re-installing the key file from Windows Explorer (right click on the PFX file and click Install)
- Installing Visual Studio 2010 on a fresh machine for the first time prompts you for the password the first time you open the project, and then it works. On machines upgraded from Visual Studio 2008, you don't get this option.
I've tried using the SN.EXE utility (Strong Name Tool) to register the key with the Strong Name CSP as the error message suggests, but whenever I run the tool with any options using the version that came with Visual Studio 2010, SN.EXE just lists its command line arguments instead of doing anything. This happens regardless of what arguments I supply.
Why is this happening, and what are clear steps to fix it? I'm about to give up on ClickOnce installs and Microsoft code signing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(27)
我也遇到了这个问题。
我能够通过运行
sn -i解决该问题
(将密钥对安装到指定容器中)。sn
通常作为 Windows SDK 的一部分安装。例如C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\sn.exe
。该位置很可能不在您的标准环境的搜索路径上。但是,Visual Studio 安装的“开发人员命令提示符”会添加其他信息,通常包括正确的位置。根据您的帖子,看起来像
sn -i companyname.pfx VS_KEY_3E185446540E7F7A
如果您在 VS 2010 中加载了解决方案,则必须从 PFX 文件的位置运行您只需右键单击解决方案资源管理器中的 pfx 文件,然后选择“打开命令提示符”,这将在正确的目录中启动 .net 2010 cmd 提示符工具。
在运行此 sn 命令之前,我通过右键单击 pfx 并选择安装来重新安装 pfx,但这不起作用。只是需要注意一点,因为它可能是两者的组合提供的解决方案。
I was running into this problem as well.
I was able to resolve the issue by running
sn -i <KeyFile> <ContainerName>
(installs key pair into a named container).sn
is usually installed as part of a Windows SDK. For exampleC:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\sn.exe
. Most likely this location is not on the search path for your standard environment. However, the "Developer Command Prompt" installed by Visual Studio adds additional information that usually includes the correct location.Based on your post that would look like
sn -i companyname.pfx VS_KEY_3E185446540E7F7A
This must be run from the location of your PFX file, if you have the solution loaded in VS 2010 you can simply right click on the pfx file from the solution explorer and choose Open Command Prompt which will launch the .net 2010 cmd prompt tool in the correct directory.
Before running this sn command I did re-install the pfx by right clicking on it and choosing install however that did not work. Just something to note as it might be the combination of both that provided the solution.
我发现了一个修复程序,可以帮助您在多开发人员环境中成功构建:
不要更改密码(这会导致 .pfx 更改),而是从组合框。然后调用密码对话框。输入密码后,项目将构建成功。每个开发人员都可以在本地计算机上执行此操作,而无需实际修改 .pfx 文件。
我在构建服务器计算机上对程序集进行签名时仍然遇到问题。我在那里遇到了同样的错误,但是使用 sn.exe -i 方法并不能解决构建服务器的问题。
I've discovered a fix that helps you be able to build successfully in a multi developer environment:
Instead of changing the password (which causes the .pfx to be changed), reselect the .pfx file from the combobox. This then invokes the password dialog. After entering the password, the project will build OK. Every dev can do this on his local machine without actually modifying the .pfx file.
I'm still having problems getting assemblies to be signed on our build server machine. I'm getting the same error there, however using the sn.exe -i method does not fix the problem for the buildserver.
我遇到了同样的问题,删除商店和阅读不起作用。我必须执行以下操作。
获取OpenSSL 的副本。它适用于 Windows。或者使用 Linux 盒子,因为他们几乎都有。
运行以下命令导出到密钥文件:
然后在项目属性中您可以使用 PFX 文件。
I had the same issue and deleting the store and reading didn't work. I had to do the following.
Get a copy of OpenSSL. It is available for Windows. Or use a Linux box as they all pretty much all have it.
Run the following to export to a key file:
Then in the project properties you can use the PFX file.
我说得太早了!重建使错误重新发挥作用...
我发现这有效 - 右键单击 解决方案资源管理器 并将其排除在外该项目。单击显示所有文件,右键单击,现在再次将其包含在项目中。现在撤消待处理的更改...
出于某种原因,这为我解决了这个问题并且相对轻松!
I spoke too soon! Rebuild brought the errors back into play...
I found this works - right click in Solution Explorer and exclude it from the project. Click Show all files, right click and now include it in the project again. Now undo pending changes...
For some reason this sorted it out for me and was relatively painless!
我发现在某些情况下,您应该在安装之前删除此密钥。因此,请执行以下操作:
sn -d VS_XXXX
sn -i mykey.pfx VS_XXX
I found that in some cases you should try to delete this key before you install it. So do the following:
sn -d VS_XXXX
sn -i mykey.pfx VS_XXX
VSCommands 2010(Visual Studio 插件)可以自动为您修复此问题 - 只需右键单击错误,然后从菜单中单击“应用修复”即可。您可以从 Visual Studio 库获取它。
VSCommands 2010 (plugin for Visual Studio) can fix this for you automatically - just right-click on error and click Apply Fix from the menu. You can get it from Visual Studio gallery.
在尝试了所有这些解决方案(以及更多)之后,我发现问题出在其他地方。对于购买证书后经历与我同样痛苦的人,我将分享我的问题的解决方案。
行为
据我了解,“签名”对 DLL 或 EXE 应用强名称而不是验证码。这就是为什么signtool在这种情况下可以工作,但Visual Studio中的“sign”不起作用。
原因
过去我曾使用过 Verisign 的证书。它们的证书中有一个
KeySpec=2
- 与 Visual Studio 中的“签名”功能一起使用。这些证书适用于 Visual Studio 和 Signtool。我现在从 Comodo 购买了证书,该证书的代码签名证书中的
KeySpec=1
不正确。这意味着这些证书可以与signtool(authenticode)一起正常工作,但不能与强命名(符号下拉列表)一起使用。解决方案
有两种方法可以解决此问题:
sn -k [name].snk
为您的强名称创建单独的证书。使用 snk 对程序集进行签名,然后使用带有代码签名证书的 Signtool 来使用authenticode 签名对 DLL/EXE 进行签名。虽然这看起来很奇怪,但据我了解,这是处理证书的正确方法,因为强名称与authenticode有不同的目的(另请参见此链接了解其工作原理的详细信息)。KeySpec=2
。其过程是 详细信息请参见此处。因为我想使用多个强名称,所以我当前使用选项 (1),尽管选项 (2) 也可以。
为了确保该解决方案将来不会丢失,以下是解决方案 2 的过程:
KeySpec=1
) 导出到 PFX 文件。注意:如果您确实想安全起见,请将此文件备份到安全位置并测试该文件是否可以在另一台计算机上正常导入!certutil -importPFX -userAT_SIGNATURE
您现在应该拥有带有
KeySpec=2
的密钥集/证书。如果需要,您现在可以再次使用 MMC 将其导出到另一个 PFX 文件中。After trying all these solutions (and a lot more), I found that the problem lies somewhere else. For people that go through the same misery as me after buying a certificate, I'll share the solution for my problem.
Behavior
I understand that 'sign' applies a strong name and not an authenticode to a DLL or EXE. This is why signtool will work in this case, but 'sign' in Visual studio will not work.
Reason
In the past I've had experience with certificates from Verisign. They have a
KeySpec=2
in the certificate - which is used with the 'sign' functionality in Visual Studio. These certificates work fine for both Visual Studio and signtool.I now bought certificates from Comodo, which have an incorrect
KeySpec=1
in the code signing certificates. That means these certificates work fine with signtool (authenticode) but not with strong naming (the sign drop-down).Solution
There are two ways to solve this issue:
sn -k [name].snk
. Sign the assembly using the snk and afterwards use signtool with your code signing certificate to do sign the DLL/EXE with the authenticode signature. While this seems strange, from what I understand this is a correct way to deal with certificates, because strong names have a different purpose than authenticode (see also this link for details on how this works).KeySpec=2
. The procedure for this is detailed here.Because I want to use multiple strong names, I currently use option (1), although option (2) also works.
To ensure this solution will never get lost in the future, here's the procedure of solution 2:
KeySpec=1
) to a PFX file. Note: Please backup this file to a safe location and test if the file can be imported ok on another machine if you really want to play it safe!certutil -importPFX -user <pfxfilename> AT_SIGNATURE
You now should have a keyset/certificate with
KeySpec=2
. If needed you can now export this into another PFX file using the MMC again.为了在 Visual Studio 2012 中解决此问题,我右键单击项目,属性 -> “签名”,然后取消选中“签署 ClickOnce 清单”。
To resolve this problem in Visual Studio 2012, I right click the project, properties -> "signing", and then uncheck the "Sign the ClickOnce manifests".
我在“选择强名称密钥文件”下拉框中重新选择了密钥(pfx)文件,然后在“输入密码”弹出窗口中提供了密码。保存了我的项目并且rebuild.build成功了。
< img src="https://i.sstatic.net/wKwRU.png" alt="在此处输入图像描述">
保存您的项目并进行重建。
如果收到错误消息:“尝试引用不存在的令牌”,请忽略它并继续以下步骤
单击“更改密码”按钮:
全部输入原始密码三个框,然后单击“确定”。如果您想更改密码(或者您的旧密码不符合复杂性要求),您现在就可以这样做。
保存您的项目并进行重建。
更多信息..< /a>
I reselected the Key(pfx) file in the "Choose a Strong Name Key File" drop-down box, Then provided password in the "ENTER PASSWORD" Popup Window. Saved my project and did rebuild.build succeeded.
Save your project and do a rebuild.
If get error message:”An attempt was made to reference a token that does not exist” just ignore it and Continue the below steps
Click the ‘Change Password” button:
Enter the original password in all three boxes and click OK. If you’d like to change your password (or if your old password doesn’t meet complexity requirements), you can do so now.
Save your project and do a rebuild.
More Info..
作为解决方法,我尝试以管理员身份运行 Visual Studio 2010,它对我有用。
我希望这有帮助。
As a workaround, I tried running the Visual Studio 2010 as an administrator, and it worked for me.
I hope this helps.
作为解决连接错误报告的原始作者,此消息有两种变体(我后来发现)
对于一种变体,您使用 sn.exe(通常如果您正在执行强命名)将密钥导入到强大的命名商店。
使用 certmgr 导入的另一个变体是当您针对单击一次部署之类的事情进行协同设计时(请注意,您可以将相同的证书用于这两个目的)。
希望这有帮助。
As the original author of the work around on the connect bug report, there are TWO variants of this message (I've discovered later)
For one variant you use sn.exe (usually if you are doing strong naming) to import the key to the strong naming store.
The other variant for which you use certmgr to import is when you're codesigning for things like click-once deployment (note you can use the same cert for both purposes).
Hope this helps.
没有什么对我有用,但后来我去查看了证书管理器(mmc.exe)。个人商店中没有导入证书,所以我手动导入,然后编译项目。
请参阅ClickOnce 清单签名和强- 使用 Visual Studio 项目设计器的签名页面命名程序集签名,签名程序集。
Nothing worked for me, but then I went and looked into the certificate manager (mmc.exe). The certificate was not imported in the personal store, so I imported it manually and then the project compiled.
See ClickOnce Manifest Signing and Strong-Name Assembly Signing Using Visual Studio Project Designer's Signing Page, Signing Assemblies.
这解决了我的问题:
打开您的 VS 项目
双击 Package.appxmanifest
转到“打包”选项卡
单击选择证书
单击配置证书
从文件中选择并使用 unity 或其他创建的 example.pfx
This Solved my problem:
Open your VS Project
Double click on Package.appxmanifest
Go to Packaging tab
click choose certificate
click configure certificate
select from file and use example.pfx that unity or anything else created
在组合框中重新选择密钥文件并输入密码对我们有帮助。
但是每次密钥文件变化都需要做一次,似乎不太行。
Reselecting key file in a combobox and entering password helps on this for us.
But it is needed to do each time the key file changes and it seems to be not OK.
将 Windows 安装移动到 SSD 后,我遇到了同样的问题。其他解决方案都不适合我。
我的解决方案是在 记事本 中打开项目文件并删除对 PFX 密钥的所有引用。保存文件后,在 Visual Studio 中打开解决方案。进入项目->属性->签名。您不应看到“选择强名称密钥文件”组合框中列出的任何密钥。在组合框中,浏览到该密钥,选择它,现在就可以构建您的项目了。
I had the same problem after moving my Windows installation to an SSD. None of the other solutions worked for me.
My solution was to open the project file in Notepad and remove all references to PFX keys. Once the file is saved, open the solution in Visual Studio. Go to project -> Properties -> Signing. You shouldn't see any keys listed in the 'choose the strong name key file' combo box. In the combo box, browse to the key, select it and your project can now be built.
我的问题是,TFS 构建控制器作为网络服务运行,由于某种原因,我不明白为什么没有使用 Visual Studio 构建主机服务证书。我将 Visual Studio Build 服务的标识更改为更易于管理的标识,确保它在 TFS 服务器上具有权限,并使用 MMC 手动添加证书。
问题还在于 MSBuild 无法将受密码保护的证书添加到存储中。
My problem was that the TFS Build Controller was running as a network service and for some reason I didn't understand why the Visual Studio Build Host service certificates were not being used. I changed the identity of the Visual Studio Build service to something more manageable, made sure it had rights on the TFS server, and manually added the certificates using the MMC.
The problem was also that MSBuild can't add the password protected certificates to the store.
我遇到了类似的问题,但是在“强名称密钥文件”组合框中选择 pfx 并输入密码后,我仍然遇到类似的错误(没有容器名称部分):
此外,未填充“签署 ClickOnce 清单”证书信息面板。
我在我的 pfx 上执行了“从文件中选择...”,它解决了问题。
I had a similar issue, but after selecting the pfx in a "Strong name key file" ComboBox and typing the password I still got a similar error (without the container name part):
Also, the "Sign the ClickOnce manifests" certificate information panel wasn't populated.
I did "Select from File..." on my pfx, and it solved the problem.
这里描述的所有方法都没有帮助我。但是,当我从项目中删除 *.pfx 文件并将其再次添加到程序集的签名中时,我构建了我的项目,没有任何错误!我无法解释原因。但这对我有用。
All methods described here didn't help me. But when I deleted the *.pfx file from my project and added it to the assembly's signing again, I built my project with without any error! I can't explain reasons why. But it worked for me.
好吧,这对我有用。在 Visual Studio 2010 中以管理员身份打开旧的解决方案/项目,然后打开新的或复制的解决方案/项目。作为管理员,删除新的 Visual Studio 2010 解决方案/项目中复制的 pfk 文件,然后转到项目属性并取消选择它。
打开两个项目后,将其复制粘贴到新项目。转到项目属性并选择构建。我打开并关闭了 Visual Studio,并且在从新项目中删除后构建了它,然后从旧项目中复制它并选择它。当我复制该项目并尝试构建它时,我首先在本文开头收到了错误。
Okay, this worked for me. Open the old solution/project as an administrator in Visual Studio 2010 and open the new or copied solution/project. As an administrator, remove the copied pfk file in the new Visual Studio 2010 solution/project and go to project properties and unselect it.
With both projects open, copy paste to the new one. Go to project properties and select Build. I opened and closed Visual Studio and also after removing from the new project built it before copying it from the old project and selecting it. I received the error at the start of this post first up when I copied the project and tried to build it.
在我的场景中,构建服务未使用与我使用
sn.exe
导入密钥的相同用户帐户。将帐户更改为管理员帐户后,一切正常。
In my scenario the build service was not using the same user account that I imported the key with using
sn.exe
.After changing the account to my administrator account, everything is working just fine.
不幸的是,这里提到的方法对我来说不起作用。我必须在 docker 容器中注册几个 PFX,并且需要通过命令行传递密码。
于是我重新开发了
sn.exe -i;使用 C# 中的
命令,使用 RSACryptoServiceProvider。源代码和应用程序位于 GitHub 上SnInstallPfx 项目。
SnInstallPfx 应用程序接受 PFX 密钥及其密码。它自动计算密钥容器名称 (VS_KEY_*)(借自 MSBuild 源代码)并将其安装到强名称 CSP。
用法:
Unfortunately, no approached mentioned here worked for me. I have to register couple PFX in a docker container and I need to pass the password via command line.
So I re-developed the
sn.exe -i <infile> <container>
command in C# using the RSACryptoServiceProvider. The source and the app are on GitHub inthe SnInstallPfx project.
The SnInstallPfx app accepts a PFX key and its password. It computes the key container name (VS_KEY_*) automatically (borrowed from MSBuild source code) and installs it to the strong name CSP.
Usage:
对于谁使用 GitLab 运行程序:
./gitlab-runner.exe install --user ".\ENTER-YOUR- USERNAME" --password "ENTER-YOUR-PASSWORD"
(我必须先停止并卸载)sn -icertificate.pfx VS_KEY_C***6
容器名称建议在GitLab 上失败的作业输出(msbuild 输出)
For whom is using GitLab runners:
./gitlab-runner.exe install --user ".\ENTER-YOUR-USERNAME" --password "ENTER-YOUR-PASSWORD"
(I had to stop and uninstall first)sn -i certificate.pfx VS_KEY_C***6
the container name is suggested in the failed Job output on GitLab (msbuild output)
我知道这是一个老问题,但大多数答案不再适用于其他 DevOps 系统。 VS_KEY_xxx 在虚拟机上不同,因此我们无法使用 sn.exe。
这是我在 GitHub Actions 中用于构建 VSTO 项目的 yaml 脚本,该项目需要 ClickOnce。实际上只需要“run:”之后的两行powershell:
I know this is an old question but most answers do not work on other devops system anymore. VS_KEY_xxx is different on VMs so that we cannot use sn.exe.
This is the yaml script I used in GitHub Actions to build my VSTO project, which required ClickOnce. Actually only need two lines of powershell after "run:":
在我的构建计算机上,使用
sn.exe
注册证书是不够的,还需要通过证书管理器 (certmgr.exe
) 安装证书:sn.exe -i的密钥对
。sn.exe
的完整路径特定于 .NET 版本:C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX XXX Tools\sn。 exe
.certmgr.msc
打开证书管理器。您必须将其安装在将访问证书的用户下,正如@michele-mpp-marostica 在上面的回答中提到的那样
On my build machine it was not enough to register the certificate using
sn.exe
, it was also required to install the certificate through the Certificate Manager (certmgr.exe
):sn.exe -i <pfx_file> <key_container_name_from_error_message>
. The full path tosn.exe
is specific to .NET version:C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX X.X.X Tools\sn.exe
.certmgr.msc
.You must install it under the user who will be accessing the certificate as it was mentioned by @michele-mpp-marostica in his answer above
我遇到了同样的错误。
就我而言,我尝试了上述所有方法,但无法得到结果。
我终于意识到,就我的情况来说,错误的原因是证书密码没有输入或者输入错误。当我正确动态输入密码时,错误消失了。成功的
I got the same error.
In my case, I tried all of the above, but I couldn't get the result.
I finally realized that in my case, the reason for the error was that the certificate password was not entered or entered incorrectly. The error disappeared when I entered the password dynamically correctly. successful
在 Visual Studio 2019 和 .Net 5 项目中,对我来说这个问题的解决方案是从我的解决方案中排除 .pfx 文件,它立即起作用。
In Visual Studio 2019 and a .Net 5 project the solution to this problem for me was to just exclude the .pfx file from my solution and it immediately worked.
我通过更改 Visual Studio 项目的 .csproj 文件中的以下行自行解决了这个问题:
这引发了“无法导入”错误:
将值更改为 false 使错误消失。
I solved this issue for myself by changing the following line in the Visual Studio project's .csproj file:
This threw the 'cannot import' error:
Changing the value to false made the error go away.