我使用此代码将绑定添加到IIS中的网站。
public static void AddBinding(string siteId, string hostName, string domainName, string ipAddress, string certificateThumbprint)
{
using (ServerManager serverManager = new ServerManager())
{
var site = serverManager.Sites.FirstOrDefault(p => p.Id == long.Parse(siteId));
if (site == null)
{
throw new Exception($"Can't find a site with Id '{siteId}'");
}
// add http binding
var httpBinding = site.Bindings.CreateElement();
httpBinding.Protocol = "http";
httpBinding.BindingInformation = $"{ipAddress}:80:{hostName}.{domainName}";
site.Bindings.Add(httpBinding);
// add https binding
var httpsBinding = site.Bindings.CreateElement();
httpsBinding.Protocol = "https";
httpsBinding.CertificateStoreName = "MY";
httpsBinding.CertificateHash = Encoding.UTF8.GetBytes(certificateThumbprint);
httpsBinding.SslFlags = SslFlags.Sni;
httpsBinding.BindingInformation = $"{ipAddress}:443:{hostName}.{domainName}";
site.Bindings.Add(httpsBinding);
serverManager.CommitChanges();
}
除非我在已授予特权的情况下运行,否则它会在CertificateHash部分上引发访问期望异常。它实际上创建绑定,但不附加证书。我的问题是,如何从asp.net应用程序向网站添加https绑定?有没有办法告诉Windows或IIS我的应用程序可以这样做?
或者,还有其他方法可以使用https将子域添加到asp.net应用程序吗?