Skip to content

Add better certificate configuration support to WebApplication #32124

Closed
@halter73

Description

@halter73

Is your feature request related to a problem? Please describe.

Today if you want to configure a TLS certificate for the host in code, you have to configure Kestrel directly using WebApplicationBuilder.WebHost.ConfigureKestrel(...). Kestrel is the only server we ship that supports runtime configuration of certificates currently, but it would be nice to have a way to configure the certifcate(s) to be used by any IServer capable of using them.

It would also be nice if you could configure TLS certificates with just the WebApplication like you can for Urls. Certificates can be configured per-endpoint in Kestrel (and even per-server-name using SNI), but associating certificates with endpoints and server names could prove complicated. This is especially true with the multiple places in both code and config you can define Kestrel endpoints. However, maybe we could support this for all endpoints with a known URL.

To support this in a way that is testable and could at least theoretically work with other IServers we should add a server feature interface. IServerAddressesFeature is the only other server feature that I know of in wide use.
A clear and concise description of what the problem is.
Example: I am trying to do [...] but [...]

Describe the solution you'd like

I'm still not sure I like this, but here's one possibility:

namespace Microsoft.AspNetCore.Builder
{
    public class WebApplication
    {
+        public X509Certificate2? Certificate { get; set; }
+        public void LoadPfxCertificate(string pfxPath, string? password = null);
+        public void LoadPemCertificate(string pemPath, string keyPath, string? password = null);

           // Review: Do we need special helpers for store? Or is setting the X509Certificate2 easy enough?
           // Borrows signatures from existing UseHttps overloads.
+        public void LoadStoreCertificate(StoreName storeName, string subject);
+        public void LoadStoreCertificate(StoreName storeName, string subject, bool allowInvalid);
+        public void LoadStoreCertificate(StoreName storeName, string subject, bool allowInvalid, StoreLocation location);
namespace Microsoft.AspNetCore.Hosting.Server.Features
{
+    public interface IDefaultServerCertificateFeature
+    {
+        X509Certificate2? DefaultCertificate { get; set; }
+    }

The idea is this would basically override the default/dev cert, but certificates configured for a specific Kestrel endpoint would be preferred.

Usage Examples

using var app = WebApplication.Create(args);

// Load cert from file in content root using password from user secrets.
app.LoadPemCertificate("cert.pem", "cert.key", app.Configuration["Cert:Password"]);

app.Run("https://example.org");

Additional context

What I don't like about this proposal is that it's yet another possible source for default certificates. We will have to make sure the prioritization of these sources is clear as possible, but adding more sources can only make clarifying this harder.

@Tratcher @davidfowl

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedAPI was approved in API review, it can be implementedarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcenhancementThis issue represents an ask for new feature or an enhancement to an existing onefeature-minimal-hosting

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions