[英]IIS 7.x, adding an HTTPS-enabled site: SiteCollection.Add(string, string, string, byte[]) overload
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 . 我需要以编程方式添加一个IIS 7.x站点,当我应该使用默认的HTTPS / SSL绑定创建时,我会遇到困难 ,usig SiteCollection.Add(string,string,string,byte [])重载 。
Giving
https:*:80:test.localhost
https:*:443:test.localhost
as bindingInformation throws an ArgumentException with this message: The specified HTTPS binding is invalid. 将
https:*:80:test.localhost
https:*:443:test.localhost
作为bindingInformation引发带有此消息的ArgumentException: 指定的HTTPS绑定无效。
What's wrong in this binding information? 这个绑定信息有什么问题?
Thank you. 谢谢。
EDIT : I'm using Microsoft.Web.Administration assembly. 编辑 : 我正在使用Microsoft.Web.Administration程序集。
Here is what I did to create https site and it worked. 以下是我创建https网站所做的工作。 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: UPD:证书是通过以下方式从pfx文件创建的:
// 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: 据我所知, BindingInformation
没有协议:
The value of this property is a colon-delimited string that includes the IP address, port, and host name of the binding. 此属性的值是以冒号分隔的字符串,其中包含绑定的IP地址,端口和主机名。
Source: http://msdn.microsoft.com/en-us/library/microsoft.web.administration.binding.bindinginformation%28v=vs.90%29.aspx 来源: 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: 还有一个带参数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 来源: 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. 也许你应该使用Site实例提供的Binding对象,因为它提供了比SiteCollection实例更多的设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.