Пример #1
0
        int Execute(RequestNewCertificateOptions options)
        {
            int errorCode;

            if (OptionsInBadFormat(options, out errorCode))
            {
                return(errorCode);
            }

            var requestContext = InitializeRequestContext(options);

            if (requestContext == null)
            {
                return(210);
            }

            Console.Write("Initializing...");
            CertificateProvider certProvider = null;
            var client = ClientHelper.CreateAcmeClient(requestContext.Signer, requestContext.Registration);

            try
            {
                Console.WriteLine("Done.");
                string toplevel;
                if (IsSubDomainName(options.CommonName, out toplevel))
                {
                    Console.Write("Authorizing top level domain name {0}...", toplevel);
                    DnsAuthorizer.Authorize(client, requestContext.DnsProvider, toplevel);
                    Console.WriteLine("Done.");
                }
                Console.Write("Authorizing domain name {0}...", options.CommonName);
                DnsAuthorizer.Authorize(client, requestContext.DnsProvider, options.CommonName);
                Console.WriteLine("Done.");

                Console.Write("Requesting a new certificate for common name {0}...", options.CommonName);
                certProvider = CertificateProvider.GetProvider();
                var cert = CertificateClient.RequestCertificate(client, certProvider, options.CommonName);

                Console.WriteLine("Done.");
                Console.WriteLine("Exporting certificate to file...");

                var outTypeString = options.OutputType.ToString().ToLowerInvariant();
                if (string.IsNullOrWhiteSpace(options.OutputFile))
                {
                    options.OutputFile = Path.Combine(AppliationPath, string.Concat(options.CommonName, '-', DateTime.Now.ToString("yyyyMMddHHmm"), '.', outTypeString));
                }
                string dir;
                options.OutputFile = PrepareOutputFilePath(options.OutputFile, out dir);
                CertificateExporter.Export(certProvider, cert, options.OutputType, options.OutputFile);
                Console.WriteLine("Certificate has been exported as {0} format at {1}.", outTypeString, options.OutputFile);
            }
            finally
            {
                client.Dispose();
                certProvider?.Dispose();
                requestContext.Signer.Dispose();
                requestContext.DnsProvider.Dispose();
            }

            return(0);
        }