如何以编程方式设置SSL证书?

时间:2018-12-06 07:05:41

标签: asp.net ssl iis ssl-certificate

我们需要通过选择SSL证书为现有网站添加子域,但是我们收到以下错误消息。

enter image description here

但是,在IIS中创建了一个新的子域,但未选择SSL证书。

enter image description here

static void Main(string[] args)
        {           
            string p = AppDomain.CurrentDomain.BaseDirectory;
            string certPath = p + @"SSL File\IIS SSL Certificate.cer";
            string certPass = "12345";         
            X509Certificate2 certifikat = new X509Certificate2(certPath, certPass, X509KeyStorageFlags.DefaultKeySet);
            using (ServerManager manager = new ServerManager())
            {
                string siteName = "xxx";
                Site site = manager.Sites.Where(q => q.Name == siteName).FirstOrDefault();
                if (site == null)
                {
                    throw new Exception("The specified site name does not exist in IIS!");
                }
                else
                { 
                    var binding = site.Bindings.Add("*:443:", certifikat.GetCertHash(), "Personal");
                    manager.CommitChanges();                    
                }

            }
        }

0 个答案:

没有答案