[Security] Wrong authentication scheme is called after calling method with AllowAnonymous
Describe the bug
I have two authentication schemes: JwtBearer and ApiKey (a custom scheme that looks for a single ApiKey authorization header).
I have one controller with two methods: One with [Authorize(AuthenticationScheme = "ApiKey"], another with [AllowAnonymous].
If I call the anonymous method once, all subsequent calls to the ApiKey method fails with a 401, seemingly challenging with Bearer token.
If I restart the server, I can call the ApiKey method just fine.
To Reproduce
Steps to reproduce the behavior:
- Using this version of ASP.NET Core: SDK 2.2.104
- Clone this repo: https://github.com/geirsagberg/AspNetCore-AuthBug
- Run
dotnet run - Call
./reproduce-bug.sh - See that the first call gets 200 OK, but the last call gets 401 Unauthorized
Can also be reproduced in Swagger.
Expected behavior
I would expect the method with ApiKey authentication to keep using the ApiKey scheme, even after an AllowAnonymous method had been called.
Screenshots

Additional context
Add any other context about the problem here.
Include the output of dotnet --info
.NET Core SDK (reflecting any global.json):
Version: 2.2.104
Commit: 73f036d4ac
Runtime Environment:
OS Name: Windows
OS Version: 10.0.17763
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\2.2.104\
Host (useful for support):
Version: 2.2.2
Commit: a4fd7b2c84
.NET Core SDKs installed:
2.1.202 [C:\Program Files\dotnet\sdk]
2.1.300 [C:\Program Files\dotnet\sdk]
2.1.301 [C:\Program Files\dotnet\sdk]
2.1.302 [C:\Program Files\dotnet\sdk]
2.1.400 [C:\Program Files\dotnet\sdk]
2.1.401 [C:\Program Files\dotnet\sdk]
2.1.402 [C:\Program Files\dotnet\sdk]
2.1.403 [C:\Program Files\dotnet\sdk]
2.1.500 [C:\Program Files\dotnet\sdk]
2.1.503 [C:\Program Files\dotnet\sdk]
2.2.100 [C:\Program Files\dotnet\sdk]
2.2.103 [C:\Program Files\dotnet\sdk]
2.2.104 [C:\Program Files\dotnet\sdk]
.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Need to be careful that default policy does not override your controller behaviour. Also by removing default scheme in the call to AddAuthentication will mean calls will not fallback.
Why would the default policy override the behavior I specify on the action? And why only after I have called another action with AllowAnonymous?
I pulled your repo and I'm not seeing this behaviour, but something could be different. Admittedly this is what I would expect looking at the code.
2.2.100

Just downloaded the latest sdk and there is certainly a behaviour change. EDIT : But interestingly can't reproduce through Postman
Policy workaround, will need to remove default scheme and use policy name in the controller actions
services.AddAuthorization(o =>
{
o.AddPolicy("defaultPolicy", builder =>
{
builder.RequireAuthenticatedUser();
});
o.AddPolicy("jwtPolicy", builder =>
{
builder.RequireAuthenticatedUser();
builder.AuthenticationSchemes = new [] { JwtBearerDefaults.AuthenticationScheme };
});
o.AddPolicy("apiKeyPolicy", b =>
{
b.RequireAuthenticatedUser();
b.AuthenticationSchemes = new [] { ApiKeyDefaults.AuthenticationScheme };
});
});
services.AddMvc(options =>
{
options.Filters.Add(new AuthorizeFilter("defaultPolicy"));
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
@HaoK
Removing the default scheme is undesirable, as all other controllers in our project use JwtBearer, and I would like to avoid having to specify this on every controller. Also, according to the docs:
The [Authorize] attribute specifies the authentication scheme or schemes to use regardless of whether a default is configured.
So I would expect the way I do it to work. Also really confused by why it only stops working after calling the [AllowAnonymous] method. Obviously some kind of state changes, but how, and why?
My best guess is that the wrong _effectivePolicy is cached and reused, at https://github.com/aspnet/AspNetCore/blob/v2.2.2/src/Mvc/src/Microsoft.AspNetCore.Mvc.Core/Authorization/AuthorizeFilter.cs#L133. If the same AuthorizeFilter is re-used for different AuthSchemes, wouldn't that cause problems like the one I am seeing?
I was able to work around the problem by removing the global AuthorizeFilter, and instead add .RequireAuthenticatedUser() to every policy. So that points towards cache issues in AuthorizeFilter being the culprit.
Yeah this looks like it might be a bug with caching policies, can you also try turning off the AllowCombiningAuthorizeFilters to see if that also fixes the issue @geirsagberg
@HaoK I tried, but then I always get 401 on the action with [Authorize(AuthenticationSchemes = ApiKeyDefaults.AuthenticationScheme)], with a Bearer challenge, so that's not right either.
I will try creating a custom policy provider to disable policy caching.
I encountered the same issue, and also had no luck disabling AllowCombiningAuthorizeFilters. I finally managed to work around it by implementing a custom IAuthorizationPolicyProvider and replacing the default one.
@HaoK What else do you need to figure this out, and decide if it's a bug, or a bad api, or something else?
This is almost certainly a bug at this point, most likely in the caching logic of the effective policy inside of AuthorizeFilter.
This is because the workaround where you plug in a custom PolicyProvider is what disables caching
So I can definitely reproduce the issue using your repro app (on 2.2), but the good news is when I create a new app on 3.0 and copy the startup/controller/auth bits over, your repro script returns all 200. I verified using 3.0.0-preview3-19111-10
@geirsagberg can you try your app on 3.0 and confirm that its fixed in 3.0 already?
I would guess its the AuthZ/Endpoint changes that fixed this behavior in 3.0
@HaoK After following the 3.0 preview migration guide, I can no longer reproduce the error, so looks good to me. Thanks!