Configure Kestrel in .NET Core

.NET

The name Kestrel indicates the http server built in .NET core by default. Unless you use an external server to reverse proxy requests (like IIS), the HTTP calls to ASP.NET or WEBAPI will be handled by Kestrel. To set Kestrel endpoints add this to the appsettings.json


{
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://localhost:5000"
      },
      "Https": {
        "Url": "https://localhost:5001"
      }
    }
  }
}

Configure other options, such as SSL:


builder.Services.Configure<KestrelServerOptions>(options =>
{
    
    options.ConfigureHttpsDefaults(options =>
    {
        options.ServerCertificateSelector = (ConnectionContext? context, string? hostname) =>
        {
            X509Store xstore = new (StoreName.My, StoreLocation.LocalMachine);
            xstore.Open(OpenFlags.MaxAllowed);
            X509Certificate2Collection collection = xstore.Certificates.Find(X509FindType.FindBySubjectName, "localhost", false);
            if (collection.Count > 0)
            {
                return collection[0];
            }

            throw new InvalidDataException("No server certificate specified");
            
        };
        options.ClientCertificateMode = ClientCertificateMode.NoCertificate;
    });
});

Post a Comment

Previous Post Next Post