Blazor Server - Login at startup

Cenk 901 Reputation points
2022-09-02T14:23:59.953+00:00

Hello,

I am trying to set the login page as a startup of the application and if the user is not authorized then again redirect to the login page. I changed my code in app.razor as follows but still a user can enter the application. I kindly ask for your help.

Thank you.

<CascadingAuthenticationState>  
    <Router AppAssembly="@typeof(App).Assembly">  
        <Found Context="routeData">  
            <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">  
            <NotAuthorized>  
                @if (!context.User.Identity.IsAuthenticated)  
                {  
                    <RedirectToLogin />   
                }  
                else  
                {  
                    <p>You are not authorized to access this resource.</p>  
                }  
            </NotAuthorized>  
            </AuthorizeRouteView>  
            <FocusOnNavigate RouteData="@routeData" Selector="h1" />  
        </Found>  
        <NotFound>  
            <PageTitle>Not found</PageTitle>  
            <LayoutView Layout="@typeof(MainLayout)">  
                <div class="row justify-content-center">  
                    <div class="col text-center">  
                        <h2>Looks like you're lost, we didn't find anything at this location.</h2>  
                        <p>Try navigating to the Dashboard or going back in the browser.</p>  
                    </div>  
                </div>  
            </LayoutView>  
        </NotFound>  
    </Router>  
</CascadingAuthenticationState>  

  
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,102 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Chen Li - MSFT 1,201 Reputation points
    2022-09-05T01:55:10.51+00:00

    Hi @Cenk ,

    You would not need to change the code of app.razor.

    If you want to make the identity login page as your default starting page you can add @attribute [Authorize] to the top of your _Host.cshtml file. This will ensure that, if the user is not authenticated, the Login page, which is not part of the Blazor App, would be displayed even before the Blazor App is completely rendered.

    If you get the error: "The type or namespace 'Authorize' could not be found", please install the Microsoft.AspNetCore.Authorization NuGet package and add an explicit reference to it.

    @using Microsoft.AspNetCore.Authorization  
    @attribute [Authorize]  
    

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Chen Li

    1 person found this answer helpful.
    0 comments No comments

  2. Greg Wruck 1 Reputation point
    2022-10-30T20:07:30.937+00:00

    If you add this code to your Program.cs, the login page will always open first if you have not already logged in.

    builder.Services.AddAuthorization(options =>  
    {  
        // By default, all incoming requests will be authorized according to the default policy  
        options.FallbackPolicy = options.DefaultPolicy;  
    });  
    
    0 comments No comments