public void UnbindCertificate(int port)
        {
            if (_disposed)
            {
                throw new InvalidOperationException(Resources.HttpUnBindDispose);
            }

            if (!_initialized)
            {
                throw new InvalidOperationException(Resources.HttpUnBindInitialize);
            }

            var endPoint =
                new IPEndPoint(IPAddress.Any, port);

            var endPointData =
                endPoint.GetBytes();

            var endPointPtr = IntPtr.Zero;

            try
            {
                endPointPtr =
                    Marshal.AllocHGlobal(endPointData.Length);

                Marshal.Copy(endPointData, 0, endPointPtr, endPointData.Length);

                var config =
                    new HttpCertificateConfigurationSet
                {
                    IpPort = endPointPtr,
                };

                var returnCode =
                    NativeHttpServer.HttpDeleteServiceConfiguration(IntPtr.Zero, HttpConfigurationType.Certificate, ref config, (uint)Marshal.SizeOf(config), IntPtr.Zero);

                if (returnCode != 0)
                {
                    throw new Win32Exception((int)returnCode, Resources.HttpUnBindException);
                }
            }
            finally
            {
                if (endPointPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(endPointPtr);
                }
            }
        }
        public void BindCertificate(int port, X509Certificate2 certificate, StoreName storeName)
        {
            if (_disposed)
            {
                throw new InvalidOperationException(Resources.HttpBindDispose);
            }

            if (!_initialized)
            {
                throw new InvalidOperationException(Resources.HttpBindInitialize);
            }

            if (certificate == null)
            {
                throw new ArgumentNullException(Resources.HttpBindCertificate);
            }

            using (var store = new CertificateStore(new X509Store(storeName, StoreLocation.LocalMachine)))
            {
                store.Open(OpenFlags.ReadOnly);

                if (!store.Certificates.Contains(certificate))
                {
                    throw new ArgumentException();
                }
            }

            var hash =
                certificate.GetCertHash();

            var endPoint =
                new IPEndPoint(IPAddress.Any, port);

            var endPointData =
                endPoint.GetBytes();

            var hashPtr = IntPtr.Zero;

            var endPointPtr = IntPtr.Zero;

            try
            {
                endPointPtr =
                    Marshal.AllocHGlobal(endPointData.Length);

                Marshal.Copy(endPointData, 0, endPointPtr, endPointData.Length);

                hashPtr =
                    Marshal.AllocHGlobal(hash.Length);

                Marshal.Copy(hash, 0, hashPtr, hash.Length);

                var config =
                    new HttpCertificateConfigurationSet
                {
                    IpPort = endPointPtr,

                    Hash       = hashPtr,
                    HashLength = (uint)hash.Length,

                    StoreName = storeName.ToString(),

                    AppId = Guid.NewGuid()
                };

                var returnCode =
                    NativeHttpServer.HttpSetServiceConfiguration(IntPtr.Zero, HttpConfigurationType.Certificate, ref config, (uint)Marshal.SizeOf(config), IntPtr.Zero);

                if (returnCode != 0)
                {
                    throw new Win32Exception((int)returnCode, Resources.HttpBindException);
                }
            }
            finally
            {
                if (hashPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(hashPtr);
                }

                if (endPointPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(endPointPtr);
                }
            }
        }
 public static extern uint HttpDeleteServiceConfiguration(IntPtr handle, HttpConfigurationType type, ref HttpCertificateConfigurationSet configuration, uint length, IntPtr overlapped);