L
L
LiptonOlolo2021-10-21 11:38:40
Blazor
LiptonOlolo, 2021-10-21 11:38:40

A few questions about blazor server side?

Good day.
I started to study blazor (SERVER SIDE (!)) and faced with not understanding and inability to find answers to my questions in Google.
1. How to make it so that when entering the site, it immediately redirects to the authorization page, i.e. all pages of the site were available only to an authorized user;
2. Can pages be transferred to libraries? I tried to transfer the authorization page to a separate library in the Pages folder, but the application stubbornly does not see this page. It only prints "Sorry, there's nothing at this address.";

Perhaps I have a partial answer to the first question from Google, but I'm not sure if it's correct, I'm attaching:
Added an authorization attribute to _Imports.razor:
@attribute [Authorize]

In App.razor I changed the structure to:

<CascadingAuthenticationState>
    <Router AppAssembly="@typeof(Program).Assembly" PreferExactMatches="@true">
        <Found Context="routeData">
            <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
                <NotAuthorized>
                    <RedirectToLogin/>
                </NotAuthorized>
            </AuthorizeRouteView>
        </Found>
        <NotFound>
            <LayoutView Layout="@typeof(MainLayout)">
                <p>Sorry, there's nothing at this address.</p>
            </LayoutView>
        </NotFound>
    </Router>
</CascadingAuthenticationState>


where RedirectToLogin is a razor component located separately in another library (it works and redirects everything fine, but it does not see the /login page):
@inject NavigationManager navManager

@code {
    protected override void OnInitialized()
    {
        navManager.NavigateTo("login");
    }
}


Where is the /login page (located in another library along the path: MySite.SecondLib/Pages/Login.razor):
@page "/login"

<h1>LOGIN PAGE</h1>


I will be glad to any answers. Thank you.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question