我在使用Set-Authenticode
powershell函数作为Azure DevOps管道的一部分来签名.NET Standard 2.0程序集时遇到麻烦。我写了一些powershell来进行目录中的汇编并将签名应用于文件夹中的每个DLL:
$project = "${{ parameters.projects }}"; # todo: what about multi-line values here
$folderPath = [System.IO.Directory]::GetParent($project)
$files = Get-ChildItem -Path $folderPath -Filter "*.dll" -Recurse
$securePassword = ConvertTo-SecureString $(CertificatePassword) -AsPlainText -Force
$certificate = Get-PfxCertificate -FilePath $(CodeSignCertificate.secureFilePath) -NoPromptForPassword -Password $securePassword
foreach($file in $files) {
Write-Host "Setting Authenticode Signature for $file"
$result = Set-AuthenticodeSignature -FilePath $file -Certificate $certificate -Force -HashAlgorithm SHA256 -IncludeChain All -TimestampServer "http://tsa.starfieldtech.com"
if ($result.Status.ToString().Contains("Error")) { Write-Error $result.StatusMessage }
else {
Write-Host $result.Status.ToString()
Write-Host $result.StatusMessage.ToString()
}
}
该过程似乎成功完成,按照我脚本中的三行Write-Host
行,已签名的每个DLL输出以下消息:
Setting Authenticode Signature for D:\a\1\s\...\SomeAssembly.dll
Valid
Signature verified.
现在,当使用Nuget Package Explorer检查在构建末尾生成的DLL时,问题变得很明显。我看到以下错误:“文件已签名,但是签名的哈希与计算得出的哈希不匹配”。在此屏幕快照中可以看到它(将鼠标悬停在红色错误图标上时,错误会显示为工具提示)。
我也尝试过:
使用自签名证书在本地运行,看来可以正常工作。
在较早版本的构建代理上运行构建,在这种情况下,签名过程在构建期间失败,并显示错误“ Get-PfxCertificate:指定的网络密码不正确”。
使用signtool.exe。这会产生相同的错误。
我现在肯定已经没有想法了。我可能会缺少什么?
我实际上已经自己解决了。
我的代码签名管道步骤之后是一个强有力的命名步骤。强命名步骤是更改程序集的哈希,因此它不再与签名中指定的哈希匹配。
解决方案是将强命名步骤移到代码签名之前。现在,我可以成功地对程序集进行强命名,对其进行签名,然后在打包后对nuget程序包进行签名,并且所有签名在输出中均有效。