Skip to content

Commit 0e32533

Browse files
Fix www redirect canonical host
Use UriHelper.BuildAbsolute with configured canonical scheme and apex host while preserving request PathBase/Path/Query for the redirect target. This avoids relative redirect loops and keeps CodeQL-safe construction.
1 parent 53da3f8 commit 0e32533

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

EssentialCSharp.Web/Program.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using EssentialCSharp.Web.Tools;
1616
using Mailjet.Client;
1717
using Microsoft.AspNetCore.Authentication;
18+
using Microsoft.AspNetCore.Http.Extensions;
1819
using Microsoft.AspNetCore.HttpOverrides;
1920
using Microsoft.AspNetCore.Identity;
2021
using Microsoft.AspNetCore.Identity.UI.Services;
@@ -524,8 +525,15 @@ await McpJsonRpcResponseWriter.WriteErrorAsync(
524525
{
525526
if (string.Equals(context.Request.Host.Host, wwwHost, StringComparison.OrdinalIgnoreCase))
526527
{
527-
PathString redirectPath = context.Request.PathBase.Add(context.Request.Path);
528-
string redirectTarget = $"{redirectPath}{context.Request.QueryString}";
528+
HostString redirectHost = configuredBaseUri.IsDefaultPort
529+
? new HostString(apexHost)
530+
: new HostString(apexHost, configuredBaseUri.Port);
531+
string redirectTarget = UriHelper.BuildAbsolute(
532+
configuredBaseUri.Scheme,
533+
redirectHost,
534+
context.Request.PathBase,
535+
context.Request.Path,
536+
context.Request.QueryString);
529537
context.Response.Redirect(redirectTarget, permanent: true);
530538
return;
531539
}

0 commit comments

Comments
 (0)