// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Server.Kestrel.Https; using Microsoft.Extensions.Configuration; using Microsoft.Net.Http.Server; using System; using System.IO; using System.Security.Cryptography.X509Certificates; using System.Threading; namespace AspnetCoreModule.TestSites.Standard { public static class Program { private static X509Certificate2 _x509Certificate2; public static void Main(string[] args) { var config = new ConfigurationBuilder() .AddCommandLine(args) .Build(); string startUpClassString = Environment.GetEnvironmentVariable("ANCMTestStartupClassName"); IWebHostBuilder builder = null; if (!string.IsNullOrEmpty(startUpClassString)) { if (startUpClassString == "StartupHTTPS") { // load .\testresources\testcert.pfx string pfxPassword = "testPassword"; if (File.Exists(@".\TestResources\testcert.pfx")) { _x509Certificate2 = new X509Certificate2(@".\TestResources\testcert.pfx", pfxPassword); } else { throw new Exception(@"Certificate file not found: .\TestResources\testcert.pfx of which password should " + pfxPassword); } builder = new WebHostBuilder() .UseConfiguration(config) .UseIISIntegration() .UseKestrel(options => { HttpsConnectionFilterOptions httpsoptions = new HttpsConnectionFilterOptions(); httpsoptions.ServerCertificate = _x509Certificate2; httpsoptions.ClientCertificateMode = ClientCertificateMode.RequireCertificate; httpsoptions.CheckCertificateRevocation = false; options.UseHttps(httpsoptions); }) .UseStartup(); } else if (startUpClassString == "StartupCompressionCaching") { builder = new WebHostBuilder() .UseConfiguration(config) .UseIISIntegration() .UseContentRoot(Directory.GetCurrentDirectory()) .UseStartup(); } else if (startUpClassString == "StartupNoCompressionCaching") { StartupCompressionCaching.CompressionMode = false; builder = new WebHostBuilder() .UseConfiguration(config) .UseIISIntegration() .UseContentRoot(Directory.GetCurrentDirectory()) .UseStartup(); } else if (startUpClassString == "StartupHelloWorld") { builder = new WebHostBuilder() .UseConfiguration(config) .UseIISIntegration() .UseStartup(); } else if (startUpClassString == "StartupNtlmAuthentication") { builder = new WebHostBuilder() .UseConfiguration(config) .UseIISIntegration() .UseStartup(); } else { throw new System.Exception("Invalid startup class name : " + startUpClassString); } } else { builder = new WebHostBuilder() .UseConfiguration(config) .UseIISIntegration() .UseStartup(); } string startupDelay = Environment.GetEnvironmentVariable("ANCMTestStartUpDelay"); if (!string.IsNullOrEmpty(startupDelay)) { Startup.SleeptimeWhileStarting = Convert.ToInt32(startupDelay); } if (Startup.SleeptimeWhileStarting != 0) { Thread.Sleep(Startup.SleeptimeWhileStarting); } string shutdownDelay = Environment.GetEnvironmentVariable("ANCMTestShutdownDelay"); if (!string.IsNullOrEmpty(shutdownDelay)) { Startup.SleeptimeWhileClosing = Convert.ToInt32(shutdownDelay); } // Switch between Kestrel and WebListener for different tests. Default to Kestrel for normal app execution. if (string.Equals(builder.GetSetting("server"), "Microsoft.AspNetCore.Server.WebListener", System.StringComparison.Ordinal)) { if (string.Equals(builder.GetSetting("environment") ?? Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"), "NtlmAuthentication", System.StringComparison.Ordinal)) { // Set up NTLM authentication for WebListener as follows. // For IIS and IISExpress use inetmgr to setup NTLM authentication on the application or // modify the applicationHost.config to enable NTLM. builder.UseWebListener(options => { options.ListenerSettings.Authentication.AllowAnonymous = true; options.ListenerSettings.Authentication.Schemes = AuthenticationSchemes.Negotiate | AuthenticationSchemes.NTLM; }); } else { builder.UseWebListener(); } } else { builder.UseKestrel(); } var host = builder.Build(); host.Run(); if (Startup.SleeptimeWhileClosing != 0) { Thread.Sleep(Startup.SleeptimeWhileClosing); } } } }