ASP.Net core 3.1 website not redirecting when unauthorized

Ketobomb

I have an ASP.Net core 3.1 application with Identity and 2 pages: Index and Private (authorization required). When I browse to the Private page directly, I would expect the application to redirect me to the login page. Yet instead the broswer shows {"message":"Unauthorized"}. Where is that message coming from and why is it not redirecting to login?

(Navigating to login /Identity/Account/Login directly DOES show the login page)

Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));

        services.AddDefaultIdentity<BackendUser>(options => options.SignIn.RequireConfirmedAccount = true)
            .AddEntityFrameworkStores<ApplicationDbContext>();


        services.ConfigureApplicationCookie(options =>
        {
            options.LoginPath = $"/Identity/Account/Login";
            options.LogoutPath = $"/Identity/Account/Logout";
        });

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseHsts();
        app.UseStatusCodePages();
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseRouting();
        app.UseAuthentication();
        app.UseAuthorization();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
        });

    }
}

Controller

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }

    [Authorize]
    public IActionResult Private()
    {
        return View();
    }
   
}
Rami Assi

I could not reproduce your problem. You might have another problem with your project. You are using some middleware for example.

Anyway, try to logout and login again or clear the browser cookies, because the login process adds cookies to the browser to identify authentication. This will renew any broken cookies and probably will fix the authorization problem.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Redirect to login when unauthorized in ASP.NET Core

Keycloak Refresh Token Unauthorized asp .net core

SignalR in ASP.Net Core 401 Unauthorized

asp.net core middleware not redirecting

How to avoid rendering Asp.net Core razor pages when redirecting?

TempData null redirecting to View - after migrating to ASP.NET Core 3

Asp.Net Boilerplate .Net Core 2.0 AbpAuthorizationFilter - ChallengeResult / Unauthorized

Asp.Net Core custom authorization always ends with 401 Unauthorized

ASP.NET core, change default redirect for unauthorized

Customize Asp.NET Core Identity Unauthorized Response Type and Statuscode

asp.net core mvc - unauthorized ajax request

ASP.NET Core redirecting to the same page instead of HomePage

Prevent redirecting ASP.NET Core MVC Application after start

ASP .NET MVC Core + SignalR: Redirecting from a Hub to a Controller

Redirecting to log in screen if not authenticated - ASP.NET Core 2.2

ASP.NET Core log-in not redirecting me to home page

Unauthorized Error when redirecting from OAuth to application

JWT + SignalR on ASP Core 3 resulting in 401 Unauthorized

ASP.NET Core 3 Logging errors when starting Controller

Return a string instead of a view when unauthorized asp.net identity

Compiler error when trying to RenderPartialAsync with a simple ASP.NET Core website

502.5 error when deploying asp.net core website using IIS

DLL is deleted from bin when starting ASP.NET Core website for the second time

ASP.Net Core 2.0 MVC website Styles and content disappear when opening Bootstrap Modal Dialog

.NET Core JWTBearerAuth returning "Unauthorized"

How to not get "ERROR_FILE_IN_USE " error when deploying a ASP.NET Core website using FTP/shared folder?

I get a Not found error when I deploy ASP.NET CORE 2.0 WebApi to IIS under Default Website

Why is Microsoft.CodeAnalysis published with ASP.NET Core website?

301 redirect in Azure ASP.Net Core website