.NET 7 UseAuthentication and UserAuthorization ordering not respected when within UseWhen
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Describe the bug
When configuring my WebApplication I make use of a UseWhen statement so that auth logic is only applied to endpoints with a specific base path.
This worked with ASP.NET Core 6.0 but with 7.0 there are two problems:
- Auth is applied to all endpoints.
- Auth middleware runs before all other middleware.
I believe this to be caused by the new 7.0 feature that automatically calls UseAuthentication and UseAuthorization when AddAuthentication / AddAuthorization are called. I see there is code in place to prevent this if it is detected that UseAuthentication / UseAuthentication have already been called, but this does not appear to take UseWhen into account.
Expected Behavior
Auth middleware is not inserted when already inserted within a UseWhen statement.
Steps To Reproduce
app.UseWhen(
httpContext => httpContext.Request.Path.StartsWithSegments("/api"),
subApp =>
{
subApp.UseAuthentication();
subApp.UseAuthorization();
});
Exceptions (if any)
No response
.NET Version
7.0.203
Anything else?
No response
Issue Analytics
- State:
- Created 5 months ago
- Reactions:1
- Comments:10 (8 by maintainers)
Top Results From Across the Web
c# - Why does `UseAuthentication` have to be placed after ...
It looks like UseRouting is doing something that makes the file unavailable, returns 404, because when I change the order to this, moved ......
Read more >What's New in .NET 7 for Authentication and Authorization
Let's explore the new .NET 7 features for improving and simplifying authentication and authorization support in .NET applications.
Read more >ASP.NET Core Middleware
UseCors , UseAuthentication , and UseAuthorization must appear in the order shown. · UseCors currently must appear before UseResponseCaching .
Read more >The auth is not working in .net core web api?
I created a project and in startup.cs add app.UseAuthentication(); and app.UseAuthrization(); and services.AddAuthentication().
Read more >App.UseAuthorization() App.UseAuthentication() : r/dotnet
I noticed that the pre generated code has App.UseAutherization() but no App.UseAuthentication() i did look into the logic of ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yes, we should see what we can do to improve this.
Thanks for contacting us.
We’re moving this issue to the
.NET 8 Planning
milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s). If we later determine, that the issue has no community involvement, or it’s very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.