dotnet / dotnet/aspnetcore

Misleading error message when an Authorization Handler is not registered

Open
#21,607 1 comment 4 reactions 0 assignees View on GitHub
affected-few area-auth enhancement severity-minor
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Describe the bug
The error message provided when Authorization failed due to a Handler not being registered is currently:
`info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2]
Authorization failed.
info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[13]
AuthenticationScheme: Bearer was forbidden.`
This suggests there is something wrong with the Bearer Token provided, the Authentication scheme, or the default Authorization (ie. not signed in / bearer token is missing). It's especially confusing when you aren't using the [Authorize] tag at all.

The error message would be far more useful if it at least logged the usual "Dependency [HandlerName] could not be resolved" like other missing dependency errors, though maybe throwing an exception is applicable here too. The current error message is sending programmers down a rabbit hole unrelated to the actual issue.

### To Reproduce
Use the code from [this sample](https://github.com/dotnet/aspnetcore/blob/v3.1.3/src/Security/samples/CustomPolicyProvider/Startup.cs) and remove:
`services.AddSingleton();`
from Startup.cs. Try to access methods in the HomeController.

### Further technical details
- ASP.NET Core version 3.1
.NET Core SDK (reflecting any global.json):
Version: 3.1.101
Commit: b377529961

Runtime Environment:
OS Name: Windows
OS Version: 10.0.18362
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\3.1.101\

Host (useful for support):
Version: 3.1.1
Commit: a1388f194c

.NET Core SDKs installed:
3.0.100 [C:\Program Files\dotnet\sdk]
3.1.101 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
- Visual Studio 2019 community edition 16.3.2

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.