ASP.NET Core Rewrite not working as expected

39 views Asked by At

I have a RewriteExtension class (using the Microsoft.AspNetCore.Rewrite library) and have a check for this url:

https://internal.mtls.authenticate.dev.myapplication/Redirect

upon which I need to remove 'internal.mtls' so that the redirect can happen to

https://authenticate.dev.myapplication/Redirect

The redirect does happen and based on the log Redirecting to {url} (see code below) the internal.mtls was removed as expected. i.e. Redirecting to "https://authenticate.dev.myapplication/Redirect"

However, in the Redirect controller I log _httpContextAccessor.HttpContext.Request.Host.Value and the value is 'still' internal.mtls.authenticate.dev.myapplication.

My expectation is of course authenticate.dev.myapplication. What is the potential reason for the domain still containing mtls.internal after explicitly removing it?

This is the code:

 if (ShouldRedirectToTlsDomain(request))
 {
      string url = RemoveMtlsFromUrl($" 
      {request.Scheme}://{request.Host}");
      var path = request.Path.Value;
      var redirectTo = new Uri(new Uri(RemoveMtlsFromUrl(url)), 
                                     path).AbsoluteUri;
       var response = context.HttpContext.Response;
        response.StatusCode = (int)HttpStatusCode.Moved;
        Log.Information("Redirecting to {url}", redirectTo);
        response.Headers[HeaderNames.Location] = redirectTo;
}

This is the configuration in Startup

app.UseRewriter(new RewriteOptions()
 .AddRedirectToNonMtlsDomain(
     new Uri(appConfig.MtlsBaseUrl, UriKind.Absolute)));
0

There are 0 answers