aloopkin / WinCertes

An ACMEv2 client for Windows
GNU General Public License v3.0
118 stars 28 forks source link

update certificate remove IIS binding information #10

Closed anibal-acosta closed 5 years ago

anibal-acosta commented 5 years ago

When an update is made all information about the binding (ip address, port, host name) lost. To fix application should first get the current information, then delete and then create the new binding with the saved current information changing only the cert hash and cert store name

anibal-acosta commented 5 years ago

this is the idea (not tested)...

`public static bool BindCertificateForIISSite(X509Certificate2 certificate, string siteName) { if (siteName == null) return false; try { Binding CurBinding = GetBindInformation(siteName);

            RemoveHTTPSBindingFromIISSite(siteName);
            ServerManager serverMgr = new ServerManager();
            Site site = serverMgr.Sites[siteName];
            Binding binding = null;
            if (CurBinding == null)
            {
                binding = site.Bindings.Add("*:443:", certificate.GetCertHash(), "MY");
                binding.Protocol = "https";
                site.ApplicationDefaults.EnabledProtocols = "http,https";
            }
            else
            {
                CurBinding.CertificateHash = certificate.GetCertHash();
                CurBinding.CertificateStoreName = "MY";
                binding = site.Bindings.Add(CurBinding);
            }

            serverMgr.CommitChanges();
            return true;
        }
        catch (Exception e)
        {
            logger.Error($"Could not bind certificate to site {siteName}: {e.Message}");
            return false;
        }
    }

    ///added by Anibal Acosta
    /// get binding information
    private static Binding GetBindInformation(String siteName)
    {
        ServerManager serverMgr = new ServerManager();
        Site site = serverMgr.Sites[siteName];
        for (int i = 0; i < site.Bindings.Count; i++)
        {
            if (site.Bindings[i].Protocol.Equals("https"))
                return site.Bindings[i];
        }
        return null;
    }`
aloopkin commented 5 years ago

Added something that should work: 5f7f74c5029a208b5588bffcf11c6b0de92fe321

Can you have a look and see if it works in your case ?

aloopkin commented 5 years ago

Fixed in version 1.1.2