Middleware Ordering
What happens when your ASP.NET Core API registers authorization middleware before authentication middleware in the request pipeline?
A request arrives with a completely valid login token. Nothing wrong with it at all.
But authorization runs first. It finds no identity to check yet, so it rejects the request as unauthorized, token or no token.
A check that runs before its answer exists always fails. That's exactly the mistake correct middleware ordering avoids.
Fix the order, and the rest sorts itself out: move authentication so it runs first in the pipeline. Once it's first, it reads the token and attaches the caller's identity to the request, so authorization can run second, check that identity against the rule, and let the valid request through.
Middleware only sees what already ran before it. Sequence decides what each step can see.
A bouncer at a members-only lounge checks a guest's ID first, then checks the guest list for that name.
If he checked the guest list before ever looking at an ID, he wouldn't know whose name to look for. Every guest would get turned away, member or not.
Check the ID before the list, never after.