static void Main(string[] args) { var cert = new X509Certificate(); var lastModified = DateTime.MinValue; try { cert = new X509Certificate2(CertPathOnDisk, CertPassword); lastModified = File.GetLastWriteTimeUtc(CertPathOnDisk); } catch (Exception) { // ignored new certificate needs to be created } var builder443 = ServerBuilder.New().SetAddress(IPAddress.Any) .SetPort(443) .SetCertificate(cert) .SetExecutionContextFlow(ExecutionContextFlow.SuppressAlways) .SetOwinApp(env => { // This should be your owin application instead of this sample var respBody = (Stream)env["owin.ResponseBody"]; var resp = Encoding.UTF8.GetBytes("Secured content"); respBody.Write(resp, 0, resp.Length); return(Task.CompletedTask); }); var acmeCfg = new AcmeCfg(builder443, lastModified); if (acmeCfg.Domain == "example.com") { Console.WriteLine("You have to provide some real domain for this sample"); Console.ReadLine(); return; } var builder80 = ServerBuilder.New().SetAddress(IPAddress.Any) .SetPort(80) .SetExecutionContextFlow(ExecutionContextFlow.SuppressAlways) .SetOwinApp(NowinAcme.NowinAcme.Use(NowinAcme.NowinAcme.RedirectToHttps, acmeCfg)); using (var server443 = builder443.Build()) using (var server80 = builder80.Build()) { // Workaround for bug in Windows Server 2012 when ReadLine is called directly after AcceptAsync // By starting it in another thread and probably even later than calling readline it works Task.Run(() => server443.Start()); Task.Run(() => server80.Start()); Console.WriteLine("Listening on ports 80 and 443. Enter to exit."); Console.ReadLine(); } }
static void Main(string[] args) { var cert = new X509Certificate(); var lastModified = DateTime.MinValue; try { cert = new X509Certificate2(CertPathOnDisk, CertPassword); lastModified = File.GetLastWriteTimeUtc(CertPathOnDisk); } catch (Exception) { // ignored new certificate needs to be created } var builder443 = ServerBuilder.New().SetAddress(IPAddress.Any) .SetPort(443) .SetCertificate(cert) .SetExecutionContextFlow(ExecutionContextFlow.SuppressAlways) .SetOwinApp(env => { // This should be your owin application instead of this sample var respBody = (Stream)env["owin.ResponseBody"]; var resp = Encoding.UTF8.GetBytes("Secured content"); respBody.Write(resp, 0, resp.Length); return Task.CompletedTask; }); var acmeCfg = new AcmeCfg(builder443, lastModified); if (acmeCfg.Domain == "example.com") { Console.WriteLine("You have to provide some real domain for this sample"); Console.ReadLine(); return; } var builder80 = ServerBuilder.New().SetAddress(IPAddress.Any) .SetPort(80) .SetExecutionContextFlow(ExecutionContextFlow.SuppressAlways) .SetOwinApp(NowinAcme.NowinAcme.Use(NowinAcme.NowinAcme.RedirectToHttps, acmeCfg)); using (var server443 = builder443.Build()) using (var server80 = builder80.Build()) { // Workaround for bug in Windows Server 2012 when ReadLine is called directly after AcceptAsync // By starting it in another thread and probably even later than calling readline it works Task.Run(() => server443.Start()); Task.Run(() => server80.Start()); Console.WriteLine("Listening on ports 80 and 443. Enter to exit."); Console.ReadLine(); } }