当我在 .NET8 控制台项目的
$(TargetPath)
中使用 PostBuildEvent
来使用 signtool
时,我发现它指的是文件 .dll
文件而不是文件 .exe。
对
.dll
文件进行签名是有意义的,因为它是实际的程序。但是我可以使用什么 MSBuild 常量来获取目标 exe?
目前我正在这样做(为了清晰起见,将线条分开):
echo "$(SignToolPath)signtool.exe" sign
/d "$(TargetPath)"
/fd SHA256
/du "https://www.xyz.co.uk"
/f "d:\My Certificate.pfx"
/p "password"
/t "http://timestamp.sectigo.com"
/v "$(OutDir)"
echo "$(SignToolPath)signtool.exe" sign
/d "$(OutDir)\$(TargetFramework)\$(AssemblyName).exe"
/fd SHA256
/du "https://www.xyz.co.uk"
/f "d:\My Certificate.pfx"
/p "password"
/t "http://timestamp.sectigo.com"
/v "$(OutDir)"
运行评论中请求的命令,我可以看到(按此顺序):
<!-- Force .dll extension for .NETCoreApp and .NETStandard projects even if output type is exe. -->
<TargetExt Condition="'$(TargetExt)' == ''">.dll</TargetExt>
<PropertyGroup>
<TargetExt Condition="'$(TargetExt)' == '' and '$(OutputType)'=='exe'">.exe</TargetExt>
<TargetExt Condition="'$(TargetExt)' == '' and '$(OutputType)'=='winexe'">.exe</TargetExt>
<TargetExt Condition="'$(TargetExt)' == '' and '$(OutputType)'=='appcontainerexe'">.exe</TargetExt>
<TargetExt Condition="'$(TargetExt)' == '' and '$(OutputType)'=='library'">.dll</TargetExt>
<TargetExt Condition="'$(TargetExt)' == '' and '$(OutputType)'=='module'">.netmodule</TargetExt>
<TargetExt Condition="'$(TargetExt)' == '' and '$(OutputType)'=='winmdobj'">.winmdobj</TargetExt>
</PropertyGroup>
Dotnet Core 构建一个包含应用程序代码的跨平台 DLL,然后为目标平台创建一个存根可执行文件,该平台将调用 DLL 中的入口点。
https://learn.microsoft.com/en-us/dotnet/core/deploying/#publish-framework-dependent
由于您的应用程序的代码被编译为 DLL,因此
$(TargetPath)
将引用该 DLL。要确定您的应用程序是否编译为 DLL 和存根可执行文件,您可以使用 $(UseAppHost)
属性。如果创建了存根可执行文件,它将设置为 true
,否则为空。要访问存根可执行文件的路径,您可以使用 $(AssemblyName)$(_NativeExecutableExtension)
。
我通过搜索
dotnet build -pp
的输出获得了此信息。
要对输出 DLL 和存根可执行文件进行签名,我将使用如下内容:
<Target Name="Code Signing" AfterTargets="PostBuildEvent">
<PropertyGroup>
<SignTimestampUrl>http://timestamp.digicert.com</SignTimestampUrl>
<SignCertName>Name of my cert</SignCertName>
<SignToolPath>$(MSBuildProgramFiles32)\Microsoft SDKs\ClickOnce\SignTool\signtool.exe</SignToolPath>
<SignCommand>"$(SignToolPath)" sign /a /n "$(SignCertName)" /t $(SignTimestampUrl)</SignCommand>
</PropertyGroup>
<Exec Command="$(SignCommand) "$(TargetPath)"" />
<Exec Condition="$(UseAppHost) == 'true'" Command="$(SignCommand) "$(TargetDir)$(AssemblyName)$(_NativeExecutableExtension)"" />
</Target>