// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.IO; using System.Security.Cryptography.X509Certificates; using Microsoft.AspNetCore.Server.Kestrel; using Microsoft.AspNetCore.Server.Kestrel.Filter; using Microsoft.AspNetCore.Server.Kestrel.Https; using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspNetCore.Hosting { public static class KestrelServerOptionsHttpsExtensions { /// /// Configure Kestrel to use HTTPS. /// /// /// The Microsoft.AspNetCore.Server.KestrelServerOptions to configure. /// /// /// The name of a certificate file, relative to the directory that contains the application content files. /// /// /// The Microsoft.AspNetCore.Server.KestrelServerOptions. /// public static KestrelServerOptions UseHttps(this KestrelServerOptions options, string fileName) { var env = options.ApplicationServices.GetRequiredService(); return options.UseHttps(new X509Certificate2(Path.Combine(env.ContentRootPath, fileName))); } /// /// Configure Kestrel to use HTTPS. /// /// /// The Microsoft.AspNetCore.Server.KestrelServerOptions to configure. /// /// /// The name of a certificate file, relative to the directory that contains the application content files. /// /// /// The password required to access the X.509 certificate data. /// /// /// The Microsoft.AspNetCore.Server.KestrelServerOptions. /// public static KestrelServerOptions UseHttps(this KestrelServerOptions options, string fileName, string password) { var env = options.ApplicationServices.GetRequiredService(); return options.UseHttps(new X509Certificate2(Path.Combine(env.ContentRootPath, fileName), password)); } /// /// Configure Kestrel to use HTTPS. /// /// /// The Microsoft.AspNetCore.Server.KestrelServerOptions to configure. /// /// /// The X.509 certificate. /// /// /// The Microsoft.AspNetCore.Server.KestrelServerOptions. /// public static KestrelServerOptions UseHttps(this KestrelServerOptions options, X509Certificate2 serverCertificate) { return options.UseHttps(new HttpsConnectionFilterOptions { ServerCertificate = serverCertificate }); } /// /// Configure Kestrel to use HTTPS. /// /// /// The Microsoft.AspNetCore.Server.KestrelServerOptions to configure. /// /// /// Options to configure HTTPS. /// /// /// The Microsoft.AspNetCore.Server.KestrelServerOptions. /// public static KestrelServerOptions UseHttps(this KestrelServerOptions options, HttpsConnectionFilterOptions httpsOptions) { var prevFilter = options.ConnectionFilter ?? new NoOpConnectionFilter(); options.ConnectionFilter = new HttpsConnectionFilter(httpsOptions, prevFilter); return options; } } }