Search code examples
c#blazor-webassembly

Blazor Webassembly App displays blank page after Implementing Authorization


I am working on Blazor Webassembly project with .net6. Everything was working fine until I implemented authorization.

I followed tutorials in [[1]: https://blazorschool.com/tutorial/blazor-wasm/dotnet6/implementing-authorization-980267][1] to implement authorization but the app does not work as expected.

When I run the app, it renders blank page instead of login page even though I have allowed anonymous access to login page.

I could not figure out what I am doing wrong after several hours of comparing my code with that shown in the tutorials. I have also included relevant ports of my code below.

App.razor:

    <CascadingAuthenticationState>
    <Router AppAssembly="@typeof(App).Assembly" >
        <Found Context="routeData">
            <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" ></AuthorizeRouteView>
            <FocusOnNavigate RouteData="@routeData" Selector="h1" />
        </Found>
        <NotFound>
            <LayoutView Layout="@typeof(MainLayout)">
                <p>Sorry, there's nothing at this address.</p>
            </LayoutView>
        </NotFound>
    </Router>
 </CascadingAuthenticationState>

Login.razor:

@layout LoginLayout
@page "/"
@using BaseComponents
@inherits LoginBase
@attribute [AllowAnonymous]

     <EditForm Model="@user" OnValidSubmit="@ValidateUser" >

_Imports.razor:

@using Microsoft.AspNetCore.Authorization
@attribute [Authorize]
@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Components.Forms

Thanks for the usual assistance.


Solution

  • add @using Microsoft.AspNetCore.Components.Authorization to _Imports.razor