IIS 7 and Above
Configuration & Scripting
Install a certificate in IIS via C#
Last post Dec 10, 2009 03:25 PM by joe.toussaint
Dec 09, 2009 07:43 PM|joe.toussaint|LINK
I'm trying to figure out how to install a new certificate in IIS 7 via C#, ideally using the classes available in Microsoft.Web.Administration. Using the code below I've been able to create a binding, however when I look at it through IIS Manager it doesn't
have a certificate configured. I'm guessing this is because my certificate is not showing up in "Server Certificates" at the web server level.
public void InstallCertificate(string siteName, string certName, string bindingInformation)
//For some reason this created a new store called 'Personal' - we'll have to figure that out
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);
//Looks like we can create this from a byte array as well
X509Certificate2 certificate = new X509Certificate2(@"C:\certs\" + certName);
Site site = _serverManager.Sites[siteName];
if (site != null)
site.Bindings.Add(bindingInformation, certificate.GetCertHash(), store.Name);
Any help would be greatly appreciated!
Dec 10, 2009 03:25 PM|joe.toussaint|LINK
Turns out I had an issue with how I was creating my certs. I folled this advice and things worked.