I have an auth policy configured in startup.cs
services.AddAuthorization(auth =>
{
auth.AddPolicy("Bearer", policy => policy
.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
.RequireAuthenticatedUser().Build());
});
and I am using this in an attribute on an api controller:
[Authorize("Bearer")]
When I run the debugger in VS code, I get an exception :
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HL14EICAK26S": An unhandled exception was thrown by the application.
System.InvalidOperationException: The AuthorizationPolicy named: 'Bearer' was not found.
at Microsoft.AspNetCore.Authorization.AuthorizationPolicy.<CombineAsync>d__9.MoveNext()
When I run the program via dotnet run
, I don't get this exception and it's able to pick up on the authorization attribute.
Why is this happening?
Cleaned, restored and rebuilt the project ... and it magically worked again....