locked
ASP.NET Core 3.x Authorize attribute not working RRS feed

  • Question

  • User320513238 posted

    I have an ASP.NET Core 3 MVC application that I'm trying to get the Authorize attribute to work with.  In the past, I've never had an issue with it but, for some reason, in core 3 I am.

    Here is my ConfigureServices method:

            public void ConfigureServices(IServiceCollection services)
    {
    services.AddMvc();
    services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie();
    services.AddAuthorization();
    }

    And here is my Configure method:

            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                    app.UseDeveloperExceptionPage();
                else
                    app.UseExceptionHandler("/Home/Error");
    
                app.UseAuthentication();
                app.UseAuthorization();
                app.UseRouting()
                    .UseStaticFiles()
                    .UseEndpoints(endpoints =>
                    {
                        endpoints.MapControllers();
                        endpoints.MapControllerRoute(
                            name: "default",
                            pattern: "{controller=Account}/{action=Login}/{id?}");
                    });
            }

    I have a Home controller that I've placed the Authorize attribute on yet, I can still navigate to it once the application starts.

    Saturday, November 30, 2019 4:36 PM

Answers

  • User-854763662 posted

    Hi da.3vil.coder,

    The order that middleware components are added in the Startup.Configure method defines the order in which the middleware components are invoked on requests and the reverse order for the response. The order is critical for security, performance, and functionality.

    Change your middleware order in Configure method like below:

     public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
                if (env.IsDevelopment())
                    app.UseDeveloperExceptionPage();
                else
                    app.UseExceptionHandler("/Home/Error");
    
                app.UseStaticFiles();
                app.UseRouting();
    
                app.UseAuthentication();
                app.UseAuthorization();
                    
                app.UseEndpoints(endpoints =>
                    {
                        endpoints.MapControllers();
                        endpoints.MapControllerRoute(
                            name: "default",
                            pattern: "{controller=Account}/{action=Login}/{id?}");
                    });
    }

    You could refer to MS documentation on Middleware order .

    Best Regards ,

    Sherry

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 2, 2019 1:55 AM

All replies

  • User-854763662 posted

    Hi da.3vil.coder,

    The order that middleware components are added in the Startup.Configure method defines the order in which the middleware components are invoked on requests and the reverse order for the response. The order is critical for security, performance, and functionality.

    Change your middleware order in Configure method like below:

     public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
                if (env.IsDevelopment())
                    app.UseDeveloperExceptionPage();
                else
                    app.UseExceptionHandler("/Home/Error");
    
                app.UseStaticFiles();
                app.UseRouting();
    
                app.UseAuthentication();
                app.UseAuthorization();
                    
                app.UseEndpoints(endpoints =>
                    {
                        endpoints.MapControllers();
                        endpoints.MapControllerRoute(
                            name: "default",
                            pattern: "{controller=Account}/{action=Login}/{id?}");
                    });
    }

    You could refer to MS documentation on Middleware order .

    Best Regards ,

    Sherry

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 2, 2019 1:55 AM
  • User320513238 posted

    That fixed me.  Thank you so much!

    Monday, December 2, 2019 11:45 PM