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);
store.Add(certificate);
Site site = _serverManager.Sites[siteName];
if (site != null)
{
site.Bindings.Add(bindingInformation, certificate.GetCertHash(), store.Name);
}
joe.toussain...
2 Posts
Install a certificate in IIS via C#
Dec 09, 2009 06:43 PM|LINK
Hi,
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);
store.Add(certificate);
Site site = _serverManager.Sites[siteName];
if (site != null)
{
site.Bindings.Add(bindingInformation, certificate.GetCertHash(), store.Name);
}
store.Close();
}
Any help would be greatly appreciated!
thanks
joe
joe.toussain...
2 Posts
Re: Install a certificate in IIS via C#
Dec 10, 2009 02:25 PM|LINK
Turns out I had an issue with how I was creating my certs. I folled this advice and things worked.
http://stackoverflow.com/questions/496658/using-makecert-for-development-ssl
thanks
joe