The selected certificate is not valid for coded si

2019-08-06 20:34发布

问题:

I have the following Powershell script to create a new cert for a C# application:

$expirationDate = [datetime]::Today.AddYears(5)
$thumb = (New-SelfSignedCertificate -CertStoreLocation cert:\localmachine\my -DnsName $env:USERDNSDOMAIN -KeyExportPolicy Exportable -Provider "Microsoft Enhanced RSA and AES Cryptographic Provider" -NotAfter $expirationDate).Thumbprint
$pwd = '123456'
$SSpwd = ConvertTo-SecureString -String $pwd -Force -AsPlainText
$destinationDirectory = "C:\Test\" 
$filename = "mycert.pfx"
$pathAndFilename = $destinationDirectory + $filename
Export-PfxCertificate -cert "cert:\localmachine\my\$thumb" -FilePath $pathAndFilename -Password $SSpwd

It runs fine. Then in Visual Studio, in the application, on the project properties page, Signing tab, I click "Select from File" and browse to the file and I get:

The selected certificate is not valid for coded signing. Choose another certicate file.

What am I doing wrong?

Adding info based on @ConnorLSW comment. I have this script now:

$pwd = '123456'
$SSpwd = ConvertTo-SecureString -String $pwd -Force -AsPlainText
$destinationDirectory = "C:\Test\" 
$filename = "mycert.pfx"
$pathAndFilename = $destinationDirectory + $filename
Import-PfxCertificate -cert "cert:\localmachine\my" -FilePath $pathAndFilename -Password $SSpwd

which outputs:

Directory: Microsoft.PowerShell.Security\Certificate::LocalMachine\my
Thumbprint Subject
AA1032E160156EC22D4447967A8B6401FF25E838 CN=AAAA.BBBB.CC.DDD

No mention of usage there and in the documentation I see no mention of usage. How do I get this info?

回答1:

You need to run New-SelfSignedCertificate with -Type CodeSigningCert to get a certificate that's usable for code signing.

Yes, the Get-Help page for New-SelfSignedCertificate is super useless.

[Note that I cannot see your screenshot as that image host is blocked on my network.]