Ejemplo n.º 1
0
 public CertificateBuilderService(ILogger <CertificateBuilderService> logger,
                                  IOptions <LetsEncryptOptions> options,
                                  CertificateSelector certificateSelector)
 {
     _logger              = logger;
     _options             = options.Value;
     _certificateSelector = certificateSelector;
 }
 public CertificateRequestService(ILogger <CertificateRequestService> logger,
                                  IOptions <LetsEncryptOptions> options,
                                  CertificateSelector certificateSelector,
                                  AccountManager accountManager,
                                  IHttpChallengeResponseStore httpChallengeResponseStore)
 {
     _logger                     = logger;
     _options                    = options.Value;
     _certificateSelector        = certificateSelector;
     _accountManager             = accountManager;
     _httpChallengeResponseStore = httpChallengeResponseStore;
 }
Ejemplo n.º 3
0
        public static void AddLetsEncrypt(this IServiceCollection services, LetsEncryptOptions options)
        {
            if (!options.AcceptTermsOfService)
            {
                throw new Exception("You must accept Let’s Encrypt terms of service");
            }

            services.Configure <LetsEncryptOptions>(x =>
            {
                x.EmailAddress       = options.EmailAddress;
                x.CacheFolder        = options.CacheFolder;
                x.AccountKey         = options.AccountKey;
                x.EncryptionPassword = options.EncryptionPassword;
                x.DaysBefore         = options.DaysBefore;
                x.UseStagingServer   = options.UseStagingServer;
            });

            var selector = new CertificateSelector(options);

            foreach (var host in options.ConfiguredHosts)
            {
                var cert = host.FallBackCertificate;
                if (host.FallBackCertificate == null && !string.IsNullOrEmpty(options.CacheFolder))
                {
                    var fileName = Path.Combine(options.CacheFolder, host.HostName + ".pfx");
                    if (File.Exists(fileName))
                    {
                        cert = new X509Certificate2(fileName, options.EncryptionPassword);
                    }
                }

                selector.Use(host.HostName, cert);
            }

            ServiceLocator.SetCertificateSelector(selector);

            services.AddSingleton <CertificateSelector>(x => selector);
            services.AddSingleton <AccountManager>();
            services.AddSingleton <HttpChallengeResponseMiddleware>();
            services.AddSingleton <IHttpChallengeResponseStore, InMemoryHttpChallengeResponseStore>();

            services.AddTransient <IConfigureOptions <KestrelServerOptions>, KestrelOptionsSetup>();
            services.AddTransient <CertificateBuilderService>();

            services.AddHostedService <CertificateRequestService>();
        }
 public KestrelOptionsSetup(CertificateSelector certificateSelector)
 {
     _certificateSelector = certificateSelector ?? throw new ArgumentNullException(nameof(certificateSelector));
 }