You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Program.cs decides whether to add UseHttpsRedirection() by checking the server's bound addresses while the pipeline is being built. Kestrel only fills in those addresses when the app starts, so the list is always empty at that point and the middleware is never added.
varserverAddressesFeature=app.Services.GetRequiredService<IServer>().Features.Get<IServerAddressesFeature>();boolssl=options.AppMode!=AppMode.Development&&serverAddressesFeatureis not null&&serverAddressesFeature.Addresses.Any(a =>a.StartsWith("https://"));if(ssl)app.UseHttpsRedirection();
A self-hosted instance listening on both HTTP and HTTPS (for example ASPNETCORE_URLS=http://+:8080;https://+:8443 with a certificate) serves plain HTTP on the HTTP port with no redirect. Setting an HTTPS port (EX_HTTPS_PORT) has no effect.
With ASPNETCORE_URLS=http://127.0.0.1:59123 this prints Before start: [] and After start: [http://127.0.0.1:59123].
Expected
When the server has an HTTPS endpoint outside Development, HTTP requests are redirected to HTTPS.
Suggested fix
Add app.UseHttpsRedirection() outside Development without the address check. The middleware resolves the HTTPS port lazily on the first request (from HttpsRedirectionOptions.HttpsPort, the HTTPS_PORT setting, or the bound addresses) and does nothing, with a single warning, when there is no HTTPS endpoint. It runs after UseForwardedHeaders(), so requests that arrive over HTTPS at a trusted proxy are not redirected. If redirection should stay opt-in, gate it on a config value instead (for example the existing EnableSsl option) rather than on bound addresses.
Activity