将SSL证书附加到IIS服务器上主网站的子绑定

时间:2019-04-10 16:27:54

标签: c# asp.net-mvc

我已经创建了一个到主站点的子绑定,并且想要将ssl证书绑定到该子绑定。下面是我的代码

string iisPortNumber = "443";
string iisBindingProtocol = "https";
string bindingUrl = "subadmin.domain.com";
CreateBinding(iisSiteName, iisPortNumber, iisBindingProtocol, bindingUrl);

public void CreateBinding(string iisSiteName, string iisPortNumber, string iisBindingProtocol, string bindingUrl)
        {

            ServerManager serverMgr = new ServerManager();
            String xSSLCertificate = @"E:\admin.domain.com.pfx";
            String xSSLCertificatePassword = @"12345678";
            try
            {
                if (IsWebsiteExists(iisSiteName, serverMgr))
                {
                    Site siteName = serverMgr.Sites[iisSiteName];
                    if (!IsExistsBinding(bindingUrl, siteName))
                    {
                        var mysite = serverMgr.Sites.Add(bindingUrl.ToString(), iisBindingProtocol, "*:" + iisPortNumber + ":" + bindingUrl, bindingUrl);

                        X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
                        store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);
                        X509Certificate2 certificate = new X509Certificate2(xSSLCertificate, xSSLCertificatePassword);
                        store.Add(certificate);

                        var binding = mysite.Bindings.Add("*:" + iisPortNumber + ":" + bindingUrl, certificate.GetCertHash(), bindingUrl);
                        binding.Protocol = iisBindingProtocol;
                        mysite.ApplicationDefaults.ApplicationPoolName = siteName.ToString();


                        serverMgr.CommitChanges();
                        store.Close();
                    }
                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }


但是我面临一个错误。即(该文件已存在时无法创建该文件。(HRESULT的异常:0x800700B7)。有人可以帮我吗?

0 个答案:

没有答案