Install certificate .cer and pfx on setup desktop program c#

839 views Asked by At

how can i install two certificates (pfx with password) during my setup is installing on pc? I need Two certificates located on personal->certificates because desktop program is used for all users on this pc.

I'm using .net 3.5

Thanks.

enter image description here

1

There are 1 answers

0
Kitson88 On

This below will extract the Public & Private key from the .PFX file and parse it into an X509Certificate2 object (X509Certificate type does not support Private keys and is unable understand V2 & V3 properties). You then pass X509Certificate2 object to the local certificate repository which is currently set to LocalMachine as I'm guessing that's where you want it according to the image you attached.

X509Certificate2 cert = new X509Certificate2(@"C:\key.pfx", "test1234", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet); //Saves in the local machine store - accessible for all users

  using (var store = new X509Store(StoreName.My, StoreLocation.LocalMachine))
  {
     store.Open(OpenFlags.ReadWrite); //Set to Write - You need Admin Permissions
     store.Add(cert); //Add Private Cert to Store
  }

I recommend that you read this post by Paul Stovell before diving head first as permissions could be nightmare especially within a domain environment (Active Directory).