How to programmatically add x509 certificate to local machine store using c#

Posted by David on Stack Overflow See other posts from Stack Overflow or by David
Published on 2013-06-24T15:51:35Z Indexed on 2013/06/24 16:21 UTC
Read the original article Hit count: 1879

Filed under:
|
|
|

I understand the question title may be a duplicate but I have not found an answer for my situation yet so here goes;

I have this simple peice of code

// Convert the Filename to an X509 Certificate
X509Certificate2 cert = new X509Certificate2(certificateFilePath);

// Get the server certificate store
X509Store store = new X509Store(StoreName.TrustedPeople, StoreLocation.LocalMachine);

store.Open(OpenFlags.MaxAllowed);
store.Add(cert); // x509 certificate created from a user supplied filename

But keep being presented with an "Access Denied" exception.

I have read some information that suggests using StorePermissions would solve my issue but I don't think this is relevant in my code. Having said that, I did test it to to be sure and I couldn't get it to work.

I also found suggestions that changing folder permissions within Windows was the way to go and while this may work(not tested), it doesn't seem practical for what will become distributed code.

I also have to add that as the code will be running as a service on a server, adding the certificates to the current user store also seems wrong.

Is there anyway to programmatically add a certificate into the local machine store?

© Stack Overflow or respective owner

Related posts about c#

Related posts about certificate