/// <summary> /// Configure Kestrel to use HTTPS. /// </summary> /// <param name="listenOptions">The <see cref="ListenOptions"/> to configure.</param> /// <param name="httpsOptions">Options to configure HTTPS.</param> /// <returns>The <see cref="ListenOptions"/>.</returns> public static ListenOptions UseHttps(this ListenOptions listenOptions, HttpsConnectionAdapterOptions httpsOptions) { var loggerFactory = listenOptions.KestrelServerOptions?.ApplicationServices.GetRequiredService <ILoggerFactory>() ?? NullLoggerFactory.Instance; listenOptions.IsTls = true; listenOptions.Use(next => { // Set the list of protocols from listen options httpsOptions.HttpProtocols = listenOptions.Protocols; var middleware = new HttpsConnectionMiddleware(next, httpsOptions, loggerFactory); return(middleware.OnConnectionAsync); }); return(listenOptions); }
// Use Https if a default cert is available internal static bool TryUseHttps(this ListenOptions listenOptions) { var options = new HttpsConnectionAdapterOptions(); listenOptions.KestrelServerOptions.ApplyHttpsDefaults(options); listenOptions.KestrelServerOptions.ApplyDefaultCert(options); if (options.ServerCertificate == null && options.ServerCertificateSelector == null) { return(false); } listenOptions.UseHttps(options); return(true); }
/// <summary> /// Configure Kestrel to use HTTPS. /// </summary> /// <param name="listenOptions">The <see cref="ListenOptions"/> to configure.</param> /// <param name="configureOptions">An action to configure options for HTTPS.</param> /// <returns>The <see cref="ListenOptions"/>.</returns> public static ListenOptions UseHttps(this ListenOptions listenOptions, Action <HttpsConnectionAdapterOptions> configureOptions) { if (configureOptions == null) { throw new ArgumentNullException(nameof(configureOptions)); } var options = new HttpsConnectionAdapterOptions(); listenOptions.KestrelServerOptions.ApplyHttpsDefaults(options); configureOptions(options); listenOptions.KestrelServerOptions.ApplyDefaultCert(options); if (options.ServerCertificate == null && options.ServerCertificateSelector == null) { throw new InvalidOperationException(CoreStrings.NoCertSpecifiedNoDevelopmentCertificateFound); } return(listenOptions.UseHttps(options)); }