4

I have a WPF Solution I am building in VSTS Online.

Below is my basic Build Definition:

enter image description here

In Visual Studio 2017 locally I've assigned a Click Once Certification (PFX). Everything builds and compiles fine locally.

enter image description here

The Build fails with the following error:

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.

What setting/step do I need to include to ensure the PFX file is seen and the password is set?

aherrick
  • 19,799
  • 33
  • 112
  • 188

1 Answers1

3

Using this code to import certificate file (PowerShell task):

$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()

Related thread: Visual studio team services deploymen/buildt certificate error

starian chen-MSFT
  • 33,174
  • 2
  • 29
  • 53
  • 1
    What value should be used for $password? I have tried the certificate password and my account password. both give Exception calling "Import" with "3" argument(s): "The specified network password is not correct." – Stuart Jun 12 '18 at 14:10
  • 1
    Should be certificate password. – starian chen-MSFT Jun 13 '18 at 03:28
  • 1
    Is this not a security risk? Won't the certificate be imported on a shared machine, including it's private key? – Jacob Nov 13 '18 at 05:06
  • I am running this as a part of Azure DevOps build pipeline as a power shell script task. The task completes without error but my build fails saying Cannot import the following key file: Image1.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_60E41B07B11F3C7E – Ziggler May 28 '19 at 18:27
  • @starianchen-MSFT not working for me. i am still getting same error message as stuart said. – Emil Dec 14 '19 at 17:00
  • @Stuart did you find the reason and solution for your problem? – Emil Dec 14 '19 at 17:00