public void SerializeHttpSysHostingOptions()
        {
            HttpSysHostingOptions s = new HttpSysHostingOptions();

            s.AllowSynchronousIO    = true;
            s.EnableResponseCaching = true;
            s.Hostname           = TestContext.CurrentContext.Random.GetString();
            s.Http503Verbosity   = Http503VerbosityLevel.Full;
            s.HttpPort           = TestContext.CurrentContext.Random.Next();
            s.HttpsPort          = TestContext.CurrentContext.Random.Next();
            s.MaxAccepts         = TestContext.CurrentContext.Random.Next();
            s.MaxConnections     = TestContext.CurrentContext.Random.Next();
            s.MaxRequestBodySize = TestContext.CurrentContext.Random.Next();
            s.Path = TestContext.CurrentContext.Random.GetString();
            s.RequestQueueLimit    = TestContext.CurrentContext.Random.Next();
            s.ThrowWriteExceptions = false;

            HttpSysHostingOptions n = JsonConvert.DeserializeObject <HttpSysHostingOptions>(JsonConvert.SerializeObject(s));

            Assert.AreEqual(s.AllowSynchronousIO, n.AllowSynchronousIO);
            Assert.AreEqual(s.EnableResponseCaching, n.EnableResponseCaching);
            Assert.AreEqual(s.Hostname, n.Hostname);
            Assert.AreEqual(s.Http503Verbosity, n.Http503Verbosity);
            Assert.AreEqual(s.HttpPort, n.HttpPort);
            Assert.AreEqual(s.HttpsPort, n.HttpsPort);
            Assert.AreEqual(s.MaxAccepts, n.MaxAccepts);
            Assert.AreEqual(s.MaxConnections, n.MaxConnections);
            Assert.AreEqual(s.MaxRequestBodySize, n.MaxRequestBodySize);
            Assert.AreEqual(s.Path, n.Path);
            Assert.AreEqual(s.RequestQueueLimit, n.RequestQueueLimit);
            Assert.AreEqual(s.ThrowWriteExceptions, n.ThrowWriteExceptions);
        }
        public void CreateNewHttpReservations(HttpSysHostingOptions originalOptions, HttpSysHostingOptions newOptions, List <Action> rollbackActions)
        {
            string httpOld  = originalOptions.BuildHttpUrlPrefix();
            string httpsOld = originalOptions.BuildHttpsUrlPrefix();
            string httpNew  = newOptions.BuildHttpUrlPrefix();
            string httpsNew = newOptions.BuildHttpsUrlPrefix();

            UrlAcl existingHttpOld = this.GetUrlReservation(httpOld);

            if (existingHttpOld != null)
            {
                UrlAcl.Delete(existingHttpOld.Prefix);
                rollbackActions.Add(() => UrlAcl.Create(existingHttpOld.Prefix, existingHttpOld.Sddl));
            }

            UrlAcl existingHttpsOld = this.GetUrlReservation(httpsOld);

            if (existingHttpsOld != null)
            {
                UrlAcl.Delete(existingHttpsOld.Prefix);
                rollbackActions.Add(() => UrlAcl.Create(existingHttpsOld.Prefix, existingHttpsOld.Sddl));
            }

            UrlAcl existingHttpNew = this.GetUrlReservation(httpNew);

            if (existingHttpNew != null)
            {
                UrlAcl.Delete(existingHttpNew.Prefix);
                rollbackActions.Add(() => UrlAcl.Create(existingHttpNew.Prefix, existingHttpNew.Sddl));
            }

            UrlAcl existingHttpsNew = this.GetUrlReservation(httpsNew);

            if (existingHttpsNew != null)
            {
                UrlAcl.Delete(existingHttpsNew.Prefix);
                rollbackActions.Add(() => UrlAcl.Create(existingHttpsNew.Prefix, existingHttpsNew.Sddl));
            }

            this.CreateUrlReservation(httpNew, this.windowsServiceProvider.ServiceSid);
            rollbackActions.Add(() => UrlAcl.Delete(httpNew));
            this.registryProvider.HttpAcl = httpNew;

            rollbackActions.Add(() => this.registryProvider.HttpAcl = httpOld);

            this.CreateUrlReservation(httpsNew, this.windowsServiceProvider.ServiceSid);
            rollbackActions.Add(() => UrlAcl.Delete(httpsNew));
            this.registryProvider.HttpsAcl = httpsNew;
            rollbackActions.Add(() => this.registryProvider.HttpsAcl = httpsOld);
        }
        public static IWebHostBuilder UseHttpSys(this IWebHostBuilder builder, IConfiguration config)
        {
            if (config.GetValueOrDefault("hosting:environment", HostingEnvironment.HttpSys) == HostingEnvironment.HttpSys)
            {
                HttpSysHostingOptions p = new HttpSysHostingOptions();
                config.Bind("Hosting:HttpSys", p);

                builder.UseHttpSys(options =>
                {
                    var mode = config.GetValueOrDefault("Authentication:Mode", AuthenticationMode.Iwa);


                    if (mode == AuthenticationMode.Iwa)
                    {
                        options.Authentication.Schemes        = config.GetValueOrDefault("Authentication:Iwa:AuthenticationSchemes", HttpSys.AuthenticationSchemes.Negotiate);
                        options.Authentication.AllowAnonymous = false;
                        options.ClientCertificateMethod       = HttpSys.ClientCertificateMethod.NoCertificate;
                    }
                    else if (mode == AuthenticationMode.Certificate)
                    {
                        options.Authentication.AllowAnonymous = true;
                        options.Authentication.Schemes        = HttpSys.AuthenticationSchemes.None;
                        options.ClientCertificateMethod       = HttpSys.ClientCertificateMethod.AllowCertificate;
                    }
                    else
                    {
                        options.Authentication.AllowAnonymous = true;
                        options.Authentication.Schemes        = HttpSys.AuthenticationSchemes.None;
                        options.ClientCertificateMethod       = HttpSys.ClientCertificateMethod.NoCertificate;
                    }

                    options.AllowSynchronousIO    = p.AllowSynchronousIO;
                    options.EnableResponseCaching = p.EnableResponseCaching;
                    options.Http503Verbosity      = (HttpSys.Http503VerbosityLevel)p.Http503Verbosity;
                    options.MaxAccepts            = p.MaxAccepts;
                    options.MaxConnections        = p.MaxConnections;
                    options.MaxRequestBodySize    = p.MaxRequestBodySize;
                    options.RequestQueueLimit     = p.RequestQueueLimit;
                    options.ThrowWriteExceptions  = p.ThrowWriteExceptions;

                    options.UrlPrefixes.Clear();
                    options.UrlPrefixes.Add(p.BuildHttpUrlPrefix());
                    options.UrlPrefixes.Add(p.BuildHttpsUrlPrefix());
                });
            }

            return(builder);
        }