Cry about...
MS-Windows Troubleshooting


error: Cannot import the following key file ...


Symptom:

When building a library in Visual Studio that is signed, the following error is generated:

Cannot import the following key file: key-file.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_123456789

Importing key file "key-file.pfx" was canceled.

Where "key-file.pfx" is the name of the key file that the import fails on and VS_KEY_123456789 the name of the key container - both of which are likely to be different and unique to your situation.

Possible Solutions

  • If you know the password for the key file then:

    A typical scenario for this is where the solution builds without error on another PC, has been checked into TFS but failed to build with this error on the local PC.

    To fix:

    1. Open the "Developer Command Prompt for VS 2019" (or the version of Visual Studio that you are using).

      Be aware that this is not the standard command prompt, even though in many respects it looks the same.

    2. CD to the folder containing the .pfx file.

      If you are not sure which folder contains the .pfx file then click on the .pfx file in the Solution Explorer and look at its properties. The "Full Path" will show the path and .pfx file name.

    3. Issue the command:

      sn -i key-file.pfx VS_KEY_123456789

      Substituting your key file name and the VS_KEY_... container name given in the error.

      It will prompt you to enter the pasword for the key file before installing the certificate.

      You should then be able to build the project.

    An alternative fix (which I have yet to try and therefore cannot confirm) is:

     to reselect the pfx file in the combo box.

    1. Open the Project Properties.
    2. On the Signing tab, reselect the .pfx file name in the combo.

      This should invoke the password dialog and allow you to enter the pasword. This should not update the certificate.

      You should then be able to build the project.

  • Gotcha: If you know the password then it is tempting to click on [Change Password ...] and reenter the password. Do not do this!

    Why? Well it will get you past the error and you will be able to build and deploy your solution. The reason you should not do this is because it will cause the key file to be updated. Should you then put this in TFS and another developer pulls it out then the other developer will experience the same error. If the other developer adopts the same solution then you will end up bouncing the error between yourselves.

  • If you do not know the password for the key file then: The only option is to create a new key file.

These notes have been tested within Microsoft Visual Studio .NET 2019 and may apply to other versions as well.



About the author: is a dedicated software developer and webmaster. For his day job he develops websites and desktop applications as well as providing IT services. He moonlights as a technical author and consultant.