Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 3rd, 2012  |  syntax: None  |  size: 1.21 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to import programmally a private key in the local machine keystore?
  2. private static void InstallCertificate(string certificatePath, string certificatePassword, StoreName store) {
  3.         try {
  4.             var serviceRuntimeUserCertificateStore = new X509Store(store, StoreLocation.LocalMachine);
  5.             serviceRuntimeUserCertificateStore.Open(OpenFlags.ReadWrite);
  6.  
  7.             X509Certificate2 cert;
  8.  
  9.             try {
  10.                 cert = new X509Certificate2(certificatePath, certificatePassword);
  11.             } catch(Exception ex) {
  12.                 Console.WriteLine("Failed to load certificate " + certificatePath);
  13.                 throw new DataException("Certificate appeared to load successfully but also seems to be null.", ex);
  14.             }
  15.  
  16.             serviceRuntimeUserCertificateStore.Add(cert);
  17.             serviceRuntimeUserCertificateStore.Close();
  18.         } catch(Exception) {
  19.             Console.WriteLine("Failed to install {0}.  Check the certificate index entry and verify the certificate file exists.", certificatePath);
  20.         }
  21.     }
  22.        
  23. cert = new X509Certificate2(certificatePath,
  24.                             certificatePassword,
  25.                             X509KeyStorageFlags.MachineKeySet);