Here is the code to add a pfx to the Cert store.
X509Store store = new X509Store( StoreName.My, StoreLocation.LocalMachine ); store.Open( OpenFlags.ReadWrite ); X509Certificate2 cert = new X509Certificate2( 'test.pfx', 'password' ); store.Add( cert ); store.Close();
However, I couldn’t find a way to set permission for NetworkService to access the private key.
Can anyone shed some light? Thanks in advance.
To do it programmatically, you have to do three things:
Get the path of the private key folder.
Get the file name of the private key within that folder.
Add the permission to that file.
See this post for some example code that does all three (specifically look at the ‘AddAccessToCertificate’ method).