Click here to Skip to main content
15,906,645 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a test certifcate that I want to install and setup for a webservice I am installing.

I install the certificate as follows

C#
X509Certificate2 cert = new X509Certificate2(Settings.CertFile, Settings.CertPass, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);

 cert.FriendlyName = Settings.CertName;

 X509Store store = new X509Store(Settings.StoreName, StoreLocation.LocalMachine);

 store.Open(OpenFlags.ReadWrite); //OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);

 store.Add(cert);

 store.Close();


This works in that the certifcate is visible in iis manager, Problem is that the certificate is not tied to the site that my webservice is installed under. I can manually bind it to the site in IIS manager by editing the site binding but I want to be able to do this in code so that I can just install and run. If you use selfssl it installs the certificate to a site using the siteID parameter, I want to mimic this in my code?
Posted

1 solution

There are a couple of solutions to this:

If using IIS7

It comes with a .net api Microsoft.Web.Administration.dll

This can be reference directly or loaded at runtime it allows you to get a sites bindings and see if a certificate is attached, remove a certificate or add one to a binding.

If you need something to work with both iis7 and iis6 then you need to us httpapi.dll which is a win32 dll so from .net requires some pinvoke work

This link proved invaluable

http://www.pinvoke.net/default.aspx/httpapi.httpqueryserviceconfiguration[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900