Fixing an UseAuthorization() error in ASP.NET Core

February 4, 2021

So recently I had this error while I was adding authorization to a web API I have been building. If you're here I assume you've had something very similar.


1System.InvalidOperationException: Endpoint TestAPI.Controllers.TestController.Get (TestAPI) contains authorization metadata, but a middleware was not found that supports authorization.
2Configure your application startup by adding app.UseAuthorization() inside the call to Configure(..) in the application startup code. The call to app.UseAuthorization() must appear between app.UseRouting() and app.UseEndpoints(...).
3   at Microsoft.AspNetCore.Routing.EndpointMiddleware.ThrowMissingAuthMiddlewareException(Endpoint endpoint)
4   at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
5   at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
6   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
7   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
8   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
9   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
10   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

It turns out, as the error suggests, I was using app.UseAuthorization() in the wrong location. I also found other posts on StackOverflow where people had the same problem. This is a very simple mistake to make as it can be rare to come across code that needs to be executed in a specific order, depending on what you build.

So how do we fix it?

The very easy fix for this is to move app.UseAuthorization() anywhere between app.UseRouting() and app.UseEndpoints() as in this example:


2//Enable Authentication
4app.UseAuthorization(); // This needs to be between app.UseRouting(); and app.UseEndpoints();
5app.UseEndpoints(endpoints =>
7    endpoints.MapControllers();

Thanks for reading! I hope this helped you. If it did, consider buying me a coffee on Ko-Fi at

Did this post help you?

Consider buying me a slice of pizza!