title: Windows service user account can't access the certificate store
body: <h1>Background</h1>
  
  <p>I have a Windows 7 VM with two user accounts (condor_usr1 and condor_usr2) that is used for source code compiling.  The condor_usr[1|2] accounts are members of the administrators group.  I have an HTCondor master VM that periodically receives jobs and assigns each job to run on one of the condor_usr[1|2] accounts.  The condor service on the Win7 VM runs as the local system account, but jobs being executed actually run as the condor_usr[1|2] account.</p>
  
  <p>I have a new requirement to sign the compiled executable.  I've imported the certificate with private key into the Current User\Personal key store in the Windows Certificate store.</p>
  
  <h1>Problem</h1>
  
  <p>If I'm logged into the Win7 VM (e.g. via remote desktop) as one of the condor_usr accounts, then compiles running as that account will successfully sign the executable, but compiles running as the other account will fail to sign the executable.  For example, if I'm logged in as condor_usr2, then compiles running under condor_usr2 will sign successfully and compiles running under condor_usr1 will fail to sign.  If I log out, both accounts fail to sign.</p>
  
  <p>The specific error I receive is:</p>
  
  <pre><code>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\VisualStudio\v15.0\OfficeTools\Microsoft.VisualStudio.Tools.Office.targets(264,9): error MSB3482: An error occurred while signing: The system cannot find the file specified.
  </code></pre>
  
  <p>Turned on some audit logging and found the following log that occurred at the same time as a signing failure.</p>
  
  <p><a href="https://i.stack.imgur.com/SzQLr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SzQLr.png" alt="signing failure"></a></p>
  
  <h1>Goal</h1>
  
  <p>Sign the compiled executable successfully regardless of which account the compile is running as and without requiring a user to be logged in.</p>
  
  <h1>What I've Figured Out So Far</h1>
  
  <ul>
  <li>The code/project being compiled is a Visual Studio 2017 solution.</li>
  <li>The signing method is ClickOnce manifest signing (an option in the VS2017 project).</li>
  <li>When a compile job is started, the job logs in as condor_usr[1|2] using logon type 2 (interactive logon).
  <a href="https://ss64.com/nt/syntax-logon-types.html" rel="nofollow noreferrer">https://ss64.com/nt/syntax-logon-types.html</a></li>
  </ul>
  
  <p><a href="https://i.stack.imgur.com/ZLT4V.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZLT4V.png" alt="logon type"></a></p>
  
  <h1>Things I've Tried</h1>
  
  <p>Unless otherwise noted, these actions had no effect and were reverted.</p>
  
  <ul>
  <li>Adding the condor_usr accounts to the Cryptographic Operators group.</li>
  <li>Importing the certificate w/ private key to the Local Computer\Personal key store.
  
  <ul>
  <li>Granting the Network Service account full control to the certificate/private key.
  <a href="https://community.dynamics.com/nav/b/technicaltipsandtricksfordynamicsnav/posts/how-to-grant-access-to-the-certificate-s-private-key-to-the-service-account-for-microsoft-dynamics-nav-server" rel="nofollow noreferrer">https://community.dynamics.com/nav/b/technicaltipsandtricksfordynamicsnav/posts/how-to-grant-access-to-the-certificate-s-private-key-to-the-service-account-for-microsoft-dynamics-nav-server</a></li>
  </ul></li>
  <li>Using <code>PsExec -h make.bat</code> to obtain the account's elevated token.
  <a href="https://docs.microsoft.com/en-us/sysinternals/downloads/psexec" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/sysinternals/downloads/psexec</a></li>
  <li>I considered that User Account Control (UAC) may be preventing the system account and/or condor_usr account from accessing the certificate store (or private keys in the cert store), but UAC is already disabled on the Win7 VM.</li>
  <li>I placed the original .pfx certificate file in the VS2017 solution and targeted it instead of the certificate in the key store.  This had no effect, which leads me to believe that the issue is some sort of signing permission rather than (or perhaps in addition to) purely permissions around the actual certificate store.</li>
  <li>I tried starting a job while logged in via remote desktop, and then logging out of the remote desktop session (actual logout, not disconnect) before the job got to the signing portion.  Signing failed.</li>
  </ul>