private static void HandleLetsEncryptRequest(IApplicationBuilder app)
 {
     app.Run(async context =>
     {
         var path  = context.Request.Path.Value;
         var token = path.Substring(WellKnownAcmeChallengePathPrefix.Length);
         var key   = AcmeChallengeTokensStorage.GetTokenKey(token);
         Console.Out.WriteLine(context.Request.Path + " :: " + key);
         await context.Response.WriteAsync(key);
     });
 }
        private static async Task CreateCertificate(string domain, string email, bool staging)
        {
            Console.Out.WriteLine("[LetsEncrypt] Logging in");
            var server = staging ? WellKnownServers.LetsEncryptStagingV2 : WellKnownServers.LetsEncryptV2;

            var(acme, account) = await GetAccount(email, server);

            Console.Out.WriteLine("[LetsEncrypt] Logged in");

            try
            {
                var orderCtx = await GetOrCreateOrder(acme, account, domain);

                var authz = (await orderCtx.Authorizations()).First();
                Console.Out.WriteLine("[LetsEncrypt] Authorizations passed");
                var httpChallenge = await authz.Http();

                Console.Out.WriteLine("[LetsEncrypt] http challenge ok");
                var token    = httpChallenge.Token;
                var keyAuthz = httpChallenge.KeyAuthz;
                AcmeChallengeTokensStorage.AddToken(token, keyAuthz);
                Console.Out.WriteLine("[LetsEncrypt] Validating token");
                await Task.Delay(5000);

                var challenge = await httpChallenge.Validate();

                while (challenge.Status != ChallengeStatus.Valid)
                {
                    Console.Out.WriteLine("[LetsEncrypt] Re-validating token");
                    await Task.Delay(1000);

                    challenge = await httpChallenge.Validate();
                }

                Console.Out.WriteLine("[LetsEncrypt] Token valid");

                var privateKey = KeyFactory.NewKey(KeyAlgorithm.ES256);
                Console.Out.WriteLine("[LetsEncrypt] Creating Csr");
                var certificationRequestBuilder = await orderCtx.CreateCsr(privateKey);

                Console.Out.WriteLine("[LetsEncrypt] Finalizing order");
                var order = await orderCtx.Finalize(certificationRequestBuilder.Generate());

                Console.Out.WriteLine("[LetsEncrypt] order.Status = " + order.Status);

                Console.Out.WriteLine("[LetsEncrypt] Downloading Certificate Chain");
                var certificateChain = await orderCtx.Download();

                Console.Out.WriteLine("[LetsEncrypt] Saving the Certificate");
                var certPfx  = certificateChain.ToPfx(privateKey);
                var password = "";
                var certData = certPfx.Build(domain, password);
                _activeCertificate = new X509Certificate2(certData, password);
                SaveCertificate(_activeCertificate, staging);
                Console.Out.WriteLine("[LetsEncrypt] Certificate saved");
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(e);
            }
        }