Attaching SSL certificate to sub bindings of main website on IIS server

0

I have created a sub binding to my main site and want to bind ssl certificate to the sub binding. Below is my code

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);
            }
        }


But i am facing an error. i.e (Cannot create a file when that file already exists. (Exception from HRESULT: 0x800700B7)). Can someone help me.

c#
asp.net-mvc
asked on Stack Overflow Apr 10, 2019 by Suvenjeet

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0