When trying to build a .NET application on our build server, it failed with the following error message:
##[error]C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(3326,5): Error MSB3326: 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)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(3326,5): error MSB3326: 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. [D:\b\3\_work\32\s\Source\Example.csproj]
##[error]C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(3326,5): Error MSB3321: Importing key file "Certificate.pfx" was canceled.
The application that is causing the problem is using a certficate to sign and publish through ClickOnce. The problem is that the certificate is stored inside a password protected pfx file. The build server cannot read the key file for that reason.
To fix the issue I had to login on the build server using the build agent service account and import the certificate manually in the personal store of the service account.
Problem solved!