diff --git a/EssentialCSharp.Web/Program.cs b/EssentialCSharp.Web/Program.cs index 0b2021af..246b9247 100644 --- a/EssentialCSharp.Web/Program.cs +++ b/EssentialCSharp.Web/Program.cs @@ -15,6 +15,7 @@ using EssentialCSharp.Web.Tools; using Mailjet.Client; using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Http.Extensions; using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.UI.Services; @@ -453,7 +454,6 @@ await context.HttpContext.Response.WriteAsync( ? configuredBaseUri.Host[4..] : configuredBaseUri.Host; string wwwHost = $"www.{apexHost}"; - string redirectAuthority = new UriBuilder(configuredBaseUri) { Host = apexHost }.Uri.GetLeftPart(UriPartial.Authority); app.UseExceptionHandler(exceptionApp => { @@ -525,8 +525,16 @@ await McpJsonRpcResponseWriter.WriteErrorAsync( { if (string.Equals(context.Request.Host.Host, wwwHost, StringComparison.OrdinalIgnoreCase)) { - string redirectUrl = $"{redirectAuthority}{context.Request.PathBase}{context.Request.Path}{context.Request.QueryString}"; - context.Response.Redirect(redirectUrl, permanent: true); + HostString redirectHost = configuredBaseUri.IsDefaultPort + ? new HostString(apexHost) + : new HostString(apexHost, configuredBaseUri.Port); + string redirectTarget = UriHelper.BuildAbsolute( + configuredBaseUri.Scheme, + redirectHost, + context.Request.PathBase, + context.Request.Path, + context.Request.QueryString); + context.Response.Redirect(redirectTarget, permanent: true); return; } await next(context);