Open Closed

Immediately direct unauthenticated users to login page #6987


User avatar
0
jhulbertpmn created
  • ABP Framework version: v8.0.4
  • UI Type: Blazor Server
  • Database System: EF Core (SQL Server)

When an unauthenticated user navigates to my application, I would like to immediately direct them to the login page, as the full application requires a user to be authenticated (no functionality for a user not logged in). I was able to get part way there by adding [Authorize] to the ComponentBase for my Blazor application - it does direct the user to the login page if they aren't authenticated - but first it briefly displays the main page with the empty menu on the left and the login button on the right, then redirects to the login page, which isn't ideal. Is it possible to immediately direct the user to the login page without displaying the main layout first?


2 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can try to check and redirect to login page in the middleware

    ....
    app.UseAuthorization();
    
    app.Use(async (context, next) =>
    {
        if(context.User.Identity?.IsAuthenticated != true && !context.Request.Path.Value.StartsWith("/Account/Login"))  
        {
            context.Response.Redirect("/Account/Login");
            return;
        }
        await next();
    });
    
    app.UseSwagger();
    
    ....
    
  • User Avatar
    0
    jhulbertpmn created

    perfect!

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
Do you need assistance from an ABP expert?
Schedule a Meeting
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v9.2.0-preview. Updated on March 13, 2025, 04:08