Saturday, January 9, 2010

Programmatically install an X.509 certificate with a private key

This c# snippet will install an X.509 certificate with a private key into your windows certificates store:


public static void InstallCertificate(string path, string password)
{
   var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
   store.Open(OpenFlags.ReadWrite);

  try
  {
     X509Certificate2 cert;

     cert = new X509Certificate2(
           path,
           password,
           X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);

     store.Add(cert);
  }
  finally
  {
     store.Close();
  }
}

What's next? get this blog rss updates or register for mail updates!

2 comments:

Sahan Arampath said...

Hey
Nice one
Keep it up...

Yaron Naveh said...

Thanks Sahan...