private void BindEndPoint( KestrelServerOptions options, IConfigurationSection endPoint, CertificateLoader certificateLoader) { var configAddress = endPoint.GetValue <string>("Address"); var configPort = endPoint.GetValue <string>("Port"); if (!IPAddress.TryParse(configAddress, out var address)) { throw new InvalidOperationException($"Invalid IP address in configuration: {configAddress}"); } if (!int.TryParse(configPort, out var port)) { throw new InvalidOperationException($"Invalid port in configuration: {configPort}"); } options.Listen(address, port, listenOptions => { var certificateConfig = endPoint.GetSection("Certificate"); X509Certificate2 certificate = null; if (certificateConfig.Exists()) { try { try { certificate = certificateLoader.Load(certificateConfig).FirstOrDefault(); } catch (KeyNotFoundException) when(certificateConfig.Value.Equals(DevelopmentSSLCertificateName, StringComparison.Ordinal) && _hostingEnvironment.IsDevelopment()) { var storeLoader = new CertificateStoreLoader(); certificate = storeLoader.Load(DefaultCertificateSubjectName, "My", StoreLocation.CurrentUser, validOnly: false) ?? storeLoader.Load(DefaultCertificateSubjectName, "My", StoreLocation.LocalMachine, validOnly: false); if (certificate == null) { var logger = _loggerFactory.CreateLogger("Microsoft.AspNetCore.KestrelOptionsSetup"); logger.LogError("No HTTPS certificate was found for development. For information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054."); } } if (certificate == null) { throw new InvalidOperationException($"No certificate found for endpoint '{endPoint.Key}'."); } } catch (Exception ex) { throw new InvalidOperationException("Unable to configure HTTPS endpoint. For information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.", ex); } listenOptions.UseHttps(certificate); } }); }
private void BindConfiguration(KestrelServerOptions options) { var certificateLoader = new CertificateLoader(_configurationRoot.GetSection("Certificates"), _loggerFactory, _hostingEnvironment.EnvironmentName); foreach (var endPoint in _configurationRoot.GetSection("Kestrel:EndPoints").GetChildren()) { BindEndPoint(options, endPoint, certificateLoader); } }