title: How to import programmally a private key in the local machine keystore?
body: <p>How to import programmally a private key in the local machine keystore?</p>
  
  <p>Actually I'm testing this code:</p>
  
  <pre><code>    private static void InstallCertificate(string certificatePath, string certificatePassword, StoreName store) {
          try {
              var serviceRuntimeUserCertificateStore = new X509Store(store, StoreLocation.LocalMachine);
              serviceRuntimeUserCertificateStore.Open(OpenFlags.ReadWrite);
  
              X509Certificate2 cert;
  
              try {
                  cert = new X509Certificate2(certificatePath, certificatePassword);
              } catch(Exception ex) {
                  Console.WriteLine("Failed to load certificate " + certificatePath);
                  throw new DataException("Certificate appeared to load successfully but also seems to be null.", ex);
              }
  
              serviceRuntimeUserCertificateStore.Add(cert);
              serviceRuntimeUserCertificateStore.Close();
          } catch(Exception) {
              Console.WriteLine("Failed to install {0}.  Check the certificate index entry and verify the certificate file exists.", certificatePath);
          }
      }
  </code></pre>
  
  <p>All tries to get the NetworkService token failed yet. The code <a href="http://geek.hubkey.com/2008/02/impersonating-built-in-service-account.html" rel="nofollow">here</a> doesn't work with admin privileges.</p>
  
  <p>The code above imports the private key to the currient user instat of the local machine. What should I do?</p>