I need to programatically add an IIS 7.x site and I got stuck when this should be created with a HTTPS/SSL binding by default, usig SiteCollection.Add(string, string, string, byte[]) overload.
Giving https:*:80:test.localhost
https:*:443:test.localhost
as bindingInformation throws an ArgumentException with this message: The specified HTTPS binding is invalid.
What's wrong in this binding information?
Thank you.
EDIT: I'm using Microsoft.Web.Administration assembly.
Here is what I did to create https site and it worked. I skip some parts of code here, of course.
using Microsoft.Web.Administration
...
using(var manager = new ServerManager())
{
// variables are set in advance...
var site = manager.Sites.Add(siteName, siteFolder, siteConfig.Port);
var store = new X509Store(StoreName.AuthRoot, StoreLocation.LocalMachine);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);
// certHash is my certificate's hash, byte[]
var binding = site.Bindings.Add("*:443:", certHash, store.Name);
binding.Protocol = "https";
store.Close();
site.ApplicationDefaults.EnabledProtocols = "http,https";
manager.CommitChanges();
}
UPD: the certificate is created from a pfx file the following way:
// get certificate from the file
string pfx = Directory.GetFiles(folder, "*.pfx", SearchOption.AllDirectories).FirstOrDefault();
var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);
var certificate = new X509Certificate2(pfx, certPassword, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
store.Add(certificate);
store.Close();
certHash = certificate.GetCertHash();
As far as I can see BindingInformation
is without the protocol:
The value of this property is a colon-delimited string that includes the IP address, port, and host name of the binding.
Source: http://msdn.microsoft.com/en-us/library/microsoft.web.administration.binding.bindinginformation%28v=vs.90%29.aspx
There is also a overload of that takes a parameter BindingProtocol:
public Site Add(
string name,
string bindingProtocol,
string bindingInformation,
string physicalPath
)
Source: http://msdn.microsoft.com/en-us/library/bb359364%28v=vs.90%29.aspx
Maybe you should use the Binding object offered by the Site instance as is offers more settings than the SiteCollection instance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With