Authorization in ASP.NET Core. Always 401 Unauthorized for [Authorize] attribute

前端 未结 7 1147
陌清茗
陌清茗 2021-01-07 18:22

For the first time I\'m creating Authorization in ASP.NET Core. I used tutorial from here TUTORIAL

The problem is when I sending request from postman:



        
7条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-07 19:20

    At the request of others here is the answer:

    The problem was with the middleware order in Startup.cs

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        ConfigureAuth(app); // your authorisation configuration
    
        app.UseMvc();
    }
    

    Why middleware order is important? If we put app.UseMvc() first - then the MVC actions would get in the routing and if they see the Authorize attribute they will take control of its handling and that's why we receives 401 Unauthorized error.

    I hope it helps someone ;)

提交回复
热议问题