-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Windows Authentication with Kestrel can ignore Authorization due to Middleware Order #14049
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
Comments
@RickStrahl I can't reproduce the behavior where IIS Express provides an identity but Kestrel doesn't, when the order of the middleware is wrong. Can you share your |
I can reproduce the IIS Express behavior of always getting an identity (even when the middleware are in the wrong order) but only if I disable anonymous authentication. This essentially forces IIS Express to always authenticate so you always get an identity flowing from IIS Express. |
@DamianEdwards - Double checked and the following is true:
Swapping the order properly makes it work on all platforms. |
Thanks @RickStrahl. To be clear, with IIS Express, when Windows authentication is enabled and anonymous disabled, it will always attempt to authenticate the user, irrespective of any challenge originating from the application. So while you may indeed end up with an identity being created, that doesn't mean the endpoint itself is being authorized if you get the middleware in the wrong order (i.e. the authentication, presence of an identity, and authorization of the endpoint, are all orthogonal). We're looking at making a slight change to the current check we have to tighten this up and have the app throw an exception in the case that an endpoint with authorization metadata is to be executed, but the authorization middleware wasn't executed in the context of an endpoint being set. Today it just checks that the endpoint middleware executed anywhere in the pipeline, but that's not enough. |
This is patch worthy |
I have the same problem using Bearer token auth
If I switch the UseRouting above the auth stuff, then the auth stuff works, otherwise all requests pass through. |
That's by design but it should throw. |
@mkArtakMSFT are we in a position to prepare this as a patch for 3.0.x, rather than waiting for 3.1? |
* Update AuthZ & Cors middlewares to only set endpoint routing metadata when executing in the context of endpoint routing * Add analyzers for incorrect UseAuth use Fixes #14049
* Update AuthZ & Cors middlewares to only set endpoint routing metadata when executing in the context of endpoint routing * Add analyzers for incorrect UseAuth use Fixes #14049
* Update AuthZ & Cors middlewares to only set endpoint routing metadata when executing in the context of endpoint routing * Add analyzers for incorrect UseAuth use Fixes #14049
* Update AuthZ & Cors middlewares to only set endpoint routing metadata when executing in the context of endpoint routing * Add analyzers for incorrect UseAuth use Fixes #14049
We'll be fixing this as part of 3.0.2 (November) release. |
Scenario:
( in certain situations)
I ended up with this scenario after using IIS Express which worked fine to produce a Windows Identity object. Switching to Kestrel (on Windows) I got the result above.
There are two problems here:
[Authorize]
After a Twitter discussion with Damien and Barry it turns out that this specific scenario was caused by a specific middleware order, but only on Kestrel. The same code below works when using IIS Express.
In
ConfigureServices()
:In
Configure()
:Note
.UseRouting()
comes after.UseAuthorization()
and.UseAuthentication()
and that's what's apparently causing the non-authorization on the controller.Switching the configuration code to:
makes the problem go away. WIth this in place both IIS Express and Kestrel now work with Windows Auth and produce a proper
WindowsIdentity
.Expected Behavior
That the middleware order needs to be a certain way is understandable if confusing and non-discoverable, although it would be nice if there was some way to notify via error or warning if the order is not viable (if possible).
The more serious problem though is that the
[Authorize]
attribute on the controller in the failure scenario fails to block the request completely. Instead it passes through the request as if there was no authorization present at all. This should fail.The text was updated successfully, but these errors were encountered: