Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically install a certificate using C#

Tags:

My school's web pages have self-trusted certificates (you must install them manually). I want to create a program that will install a certificate.cer (from Visual Studio resources) to the local user's Trusted root certificate authority.

Do you know how I can do this in C#?

like image 901
DroidBellmer Avatar asked Sep 09 '12 08:09

DroidBellmer


1 Answers

To add the certificate to the trusted root store for the current user programmatically, use the X509Store and X509Certificate2 classes. For example:

string file; // Contains name of certificate file X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser); store.Open(OpenFlags.ReadWrite); store.Add(new X509Certificate2(X509Certificate2.CreateFromCertFile(file))); store.Close(); 

See also " How can I install a certificate into the local machine store programmatically using c#? ".

Another option is the Certificate Manager command line (certmgr.exe) tool, specifically:

certmgr /add cert.cer /s Root 

where "cert.cer" is your certificate. This imports it into the trusted root store for the current user. However, certmgr.exe is part of Visual Studio and the Windows SDK and may not be freely distributable.

like image 130
akton Avatar answered Sep 22 '22 13:09

akton