Open Closed

Blazor Server - Force Authentication For Root / Entire Site #5206


User avatar
0
adam-dot-cohen created
  • ABP Framework version: v7.2.1

  • UI type: Blazor Server

  • DB provider: EF Core

  • Tiered (MVC) or Identity Server Separated (Angular): Tiered, separate OpenIddict server

How do you force authentication for the root of a blazor server project? Presently, the following display the header and navigation side menu before redirecting to Account/Login...

How do you force authentication for the root of a blazor server project? Presently, the following display the header and navigation side menu before redirecting to Account/Login...

Index.cshtml
image.png

OR

_Imports.razor
image.png

STEPS TO REPRODUCE

  1. Generate a fresh template

  2. Run the Blazor server project

RESULT
image.png

DESIRED RESULT
image.png

Please credit my account back for this question as it got locked...
https://support.abp.io/QA/Questions/5123/Blazor-Server---Force-Authentication-For-Root--Entire-Site


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

    Hi,

    It works for me:

    Steps:

    • abp new Qa -u blazor-server

    • Add @attribute [Authorize] to _Imports.razor

    image.png

    2023-06-07 09.55.02.gif

    Please credit my account back for this question as it got locked...

    Ok, we have a bot to close&lock if the question is inactive.

  • User Avatar
    0
    adam-dot-cohen created

    Thanks for your response. As I mentioned in my original question, I tried that adding the [Authorize] attribute to _Imports.razor and got the same behavior seed in the animate GIF you provided. Prior to the redirect to the authentication page, the menu and navigation load from the Layout page. The requirement I have is to secure / authorize all content on the site. If the page loads without secure content prior to redirecting, it's not secure.

    Please help! Thanks!

    abp-blazor-auth.gif

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can try :

    app.UseAuthentication(); // Add under `UseAuthentication`
    app.Use(async (httpContext, next) =>
    {
        if (!httpContext.Request.Path.ToString().Contains("account/login"))
        {
            if (httpContext.User.Identity is not { IsAuthenticated: true })
            {
                httpContext.Response.Redirect("/account/login");
                return;
            }
        }
        
    
        await next();
    });
    
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.3.0-preview. Updated on April 16, 2025, 12:13