Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UsePathBase should insert before UseRouting() in WebApplicationBuilder.ConfigureApplication #59638

Open
1 task done
wu-yafeng opened this issue Dec 25, 2024 · 0 comments
Open
1 task done
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc

Comments

@wu-yafeng
Copy link
Contributor

wu-yafeng commented Dec 25, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

I am trying use PathBaseMiddleware with WebApplicationBuilder and auth such as:

// ...
builder.Services.AddAuthorization();
builder.Services.AddAuthentication().AddCookie();
var app = builder.Build();

app.UsePathBase("/example");

// ...
app.MapControllers().RequireAuthorization();

when I access https://localhost:8080/example/api/weatherforecast, a exception page show:

InvalidOperationException: Endpoint xxx contains authorization metadata, but a middleware was not found that supports authorization. Configure your application startup by adding app.UseAuthorization() in the application startup code. If there are calls to app.UseRouting() and app.UseEndpoints(...), the call to app.UseAuthorization() must go between them.

Microsoft.AspNetCore.Routing.EndpointMiddleware.ThrowMissingAuthMiddlewareException(Endpoint endpoint)
Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
Microsoft.AspNetCore.Builder.Extensions.UsePathBaseMiddleware.InvokeCore(HttpContext context, PathString matchedPath, PathString remainingPath)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

This is becasue when I add UsePathBase(...), it will add a UseRoute after UsePathBase without Authorization.

WebApplication generate a middleware like this:

  1. UseDeveloperExceptionPage
  2. UseRouting
  3. UseAuthentication
  4. UseAuthorization
  5. WireSourcePipeline
    5.1 UsePathBase(...);
    5.2 UseRouting(...)
    5.3 other middleware user added.
  6. UseEndpoints()

The final UseEndpoints(...) middleware will execute authz. The endpoint is matched on step 5.2。step 3 will not execute because no endpoint matched on step 2.

Describe the solution you'd like

Add UsePathBase(...) to WebApplicationBuilder.ConfigureApplication(...) when environment ASPNET_PATHBASE set.

Additional context

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Dec 25, 2024
@gfoidl gfoidl added area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Dec 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc
Projects
None yet
Development

No branches or pull requests

2 participants