question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.NET 7 UseAuthentication and UserAuthorization ordering not respected when within UseWhen

See original GitHub issue

Is 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:

  1. Auth is applied to all endpoints.
  2. 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:open
  • Created 5 months ago
  • Reactions:1
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
Tratchercommented, May 9, 2023

Yes, we should see what we can do to improve this.

0reactions
msftbot[bot]commented, May 10, 2023

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found