Install Dummy Certificate for WCF Programmatically

 

Let’s assume, you have already certificates and everything works if you install them manually.

But if you try install from your c# code you may get a “Certificate keyset does not exists” from wcf host.

Using FindPrivateKey utility I’ve realized that destination folders for private key are different – my code “puts” private key to the current user store instead of “All Users”.

So, everything I need – to specify X509KeyStorageFlags.MachineKeySet flag.

The whole code is:

//install server sertificate
store = new
X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
certStream = assembly.GetManifestResourceStream(
assembly.GetName().Name + “.Resources.server.pfx”);
certByte = new
byte[certStream.Length];
certStream.Read(certByte, 0, (int)certStream.Length);
cert = new
X509Certificate2(certByte, “1234”,
    X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet
    | X509KeyStorageFlags.Exportable);
store.Add(cert);
store.Close();

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s