Mat*_*zer 10 c# iis iis-7 .net-4.0 c#-4.0
我需要以编程方式添加一个IIS 7.x站点,当我应该使用默认的HTTPS/SSL绑定创建时,我会遇到困难,usig SiteCollection.Add(string,string,string,byte [])重载.
Giving as bindingInformation会抛出一条带有此消息的ArgumentException:指定的HTTPS绑定无效.https:*:80:test.localhosthttps:*:443:test.localhost
这个绑定信息有什么问题?
谢谢.
编辑:我正在使用Microsoft.Web.Administration程序集.
ElD*_*Dog 15
以下是我创建https网站所做的工作.当然,我在这里跳过部分代码.
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();
}
Run Code Online (Sandbox Code Playgroud)
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();
Run Code Online (Sandbox Code Playgroud)