title: Signing a ClickOnce application with an AfterCompile target
body: <p>I've been trying to wrap my mind around <a href="http://en.wikipedia.org/wiki/Code_signing#Implementations" rel="noreferrer">Authenticode certificates</a> for a week now. I purchased a CSC from Comodo and I've got a ClickOnce application I'd like to sign so that the SmartScreen Filter warnings go away.</p>
  
  <p>My application assembly is strong-named and I've ticked the box to "Sign the assembly" in my Project Properties. I've also ticked the box to "Sign the ClickOnce manifest" in the same Project Properties. And finally, I have the following executions set up as AfterCompile targets in my project file, in order to dual-sign the executable with both SHA1 and SHA256:</p>
  
  <pre><code>&lt;Target Name="AfterCompile"&gt;
    &lt;Exec Command="&amp;quot;C:\Program Files (x86)\Windows Kits\8.1\bin\x64\signtool.exe&amp;quot; sign /f &amp;quot;$(ProjectDir)certificate.pfx&amp;quot; /p mypassword /t http://timestamp.comodoca.com /v &amp;quot;$(ProjectDir)obj\$(ConfigurationName)\$(TargetFileName)&amp;quot;" /&gt;
    &lt;Exec Command="&amp;quot;C:\Program Files (x86)\Windows Kits\8.1\bin\x64\signtool.exe&amp;quot; sign /f &amp;quot;$(ProjectDir)certificate.pfx&amp;quot; /p mypassword /fd sha256 /tr http://timestamp.comodoca.com/?td=sha256 /td sha256 /as /v &amp;quot;$(ProjectDir)obj\$(ConfigurationName)\$(TargetFileName)&amp;quot;" /&gt;
  &lt;/Target&gt;
  </code></pre>
  
  <p>Then I run the following the command to publish the project:</p>
  
  <blockquote>
    <p>"C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" /target:Publish /p:Configuration=Release /p:Platform=AnyCPU MyCoolApplication.csproj</p>
  </blockquote>
  
  <p>What I've noticed is that this command ultimately creates three separate versions of MyCoolApplication.exe:</p>
  
  <ul>
  <li>It builds one copy in bin\Release, which is unsigned</li>
  <li>It builds another copy in obj\Release, which is dual-signed</li>
  <li>It builds a final copy in bin\Release\app.publish, which is signed only once and appears to be missing a timestamp</li>
  </ul>
  
  <p>Unfortunately it's the copy in bin\Release\app.publish that needs to work, but for some reason this version is removing the dual signature. My understanding – which could be wrong – was that it built things in obj\Release, copied them to bin\Release\app.publish, and then signed the manifest. However clearly something else is going on, as the digital signature on the final executable is clearly changed. Here's the properties of those two files side-by-side:</p>
  
  <p><a href="https://i.stack.imgur.com/Lj4bj.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Lj4bj.png" alt="dual-signed"></a> <a href="https://i.stack.imgur.com/TIP5U.png" rel="noreferrer"><img src="https://i.stack.imgur.com/TIP5U.png" alt="single-signed, no timestamp"></a></p>
  
  <p>The problem with the final single-signed/missing timestamp version is that the application still gets flagged with the SmartScreen filter, rendering the whole process pointless. How can I fix this?</p>
  
  <p><em>UPDATE:</em> After reading <a href="https://robindotnet.wordpress.com/2013/04/14/windows-8-and-clickonce-the-definitive-answer-revisited/" rel="noreferrer">this guide</a>, it seems that even if I sign things properly, I may still bump into the SmartScreen Filter by virtue of not having enough "reputation" for my application. However I would like to confirm that I have signed things properly in the first place, and am not chasing smoke. (Or if this is indicative of a breakdown in the build process, I want to correct that!)</p>
  
  <p><em>EDIT:</em> Here is the end of the MSBuild.exe output, which @CodeFuller has requested:</p>
  
  <blockquote>
    <p><strong>AfterCompile:</strong></p>
    
    <p>"signtool.exe" sign /f "certificate.pfx" /p mypassword /t <a href="http://timestamp.comodoca.com" rel="noreferrer">http://timestamp.comodoca.com</a> /v "MyCoolApplication\obj\Release\MyCoolApplication.exe"</p>
    
    <p>The following certificate was selected:</p>
    
    <p>...</p>
    
    <p>Done Adding Additional Store</p>
    
    <p>Successfully signed: MyCoolApplication\obj\Release\MyCoolApplication.exe</p>
    
    <p>Number of files successfully Signed: 1</p>
    
    <p>Number of warnings: 0</p>
    
    <p>Number of errors: 0</p>
    
    <p>"signtool.exe" sign /f "MyCoolApp
      lication\certificate.pfx" /p mypassword /fd sha256 /tr <a href="http://timestamp.comodoca.com/?td=sha256" rel="noreferrer">http://timestamp.comodoca.com/?td=sha256</a> /td sha256 /as /v "MyCoolApplication\obj\Release\MyCoolApplication.exe"</p>
    
    <p>The following certificate was selected:</p>
    
    <p>...</p>
    
    <p>Done Adding Additional Store</p>
    
    <p>Successfully signed: MyCoolApplication\obj\Release\MyCoolApplication.exe</p>
    
    <p>Number of files successfully Signed: 1</p>
    
    <p>Number of warnings: 0</p>
    
    <p>Number of errors: 0</p>
    
    <p><strong>_DeploymentComputeClickOnceManifestInfo:</strong></p>
    
    <p>Creating directory "bin\Release\app.publish".</p>
    
    <p>Copying file from "obj\Release\MyCoolApplication.exe" to "bin\Release\app.publish\MyCoolApplication.exe".</p>
    
    <p><strong>_CopyAppConfigFile:</strong></p>
    
    <p>Copying file from "App.config" to "bin\Release\MyCoolApplication.exe.config".</p>
    
    <p><strong>_CopyManifestFiles:</strong></p>
    
    <p>Copying file from "obj\Release\MyCoolApplication.exe.manifest" to "bin\Release\MyCoolApplication.exe.manifest".</p>
    
    <p>MyCoolApplication -> C:\Users\Gordon\Documents\Visual Studio 2015\Projects\MyCoolApplication\MyCoolApplication\bin\Release\MyCoolApplication.exe.manifest</p>
    
    <p>Copying file from "obj\Release\MyCoolApplication.application" to "bin\Release\MyCoolApplication.application".</p>
    
    <p>MyCoolApplication -> C:\Users\Gordon\Documents\Visual Studio 2015\Projects\MyCoolApplication\MyCoolApplication\bin\Release\MyCoolApplication.application</p>
    
    <p><strong>CopyFilesToOutputDirectory:</strong></p>
    
    <p>Copying file from "obj\Release\MyCoolApplication.exe" to "bin\Release\MyCoolApplication.exe".</p>
    
    <p>MyCoolApplication -> C:\Users\Gordon\Documents\Visual Studio 2015\Projects\MyCoolApplication\MyCoolApplication\bin\Release\MyCoolApplication.exe</p>
    
    <p>Copying file from "obj\Release\MyCoolApplication.pdb" to "bin\Release\MyCoolApplication.pdb".</p>
    
    <p><strong>_CopyFilesToPublishFolder:</strong></p>
    
    <p>Creating directory "bin\Release\app.publish\Application Files\MyCoolApplication_1_0_0_0".</p>
    
    <p>Copying file from "bin\Release\MyCoolApplication.exe.manifest" to "bin\Release\app.publish\Application Files\MyCoolApplication_1_0_0_0\MyCoolApplication.exe.manifest".
      Copying file from "bin\Release\app.publish\MyCoolApplication.exe" to "bin\Release\app.publish\Application Files\MyCoolApplication_1_0_0_0\MyCoolApplication.exe.deploy".
      Copying file from "App.config" to "bin\Release\app.publish\Application Files\MyCoolApplication_1_0_0_0\MyCoolApplication.exe.config.deploy".
      Copying file from "triforce.ico" to "bin\Release\app.publish\Application Files\MyCoolApplication_1_0_0_0\triforce.ico.deploy".
    Done Building Project "C:\Users\Gordon\Documents\Visual Studio 2015\Projects\MyCoolApplication\MyCoolApplication\MyCoolApplication.csproj" (Publish target(s)).</p>
    
    <p>Build succeeded.
        0 Warning(s)
        0 Error(s)</p>
    
    <p>Time Elapsed 00:00:06.53</p>
  </blockquote>