Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CascadingAuthenticationState and NotFoundContent tags not working in Blazor

So I've been following this tutorial for implementing authorization/authentication on my blazor client app (asp.net core hosted). Everything worked ok until I started working on client side ("Configuring client-side Blazor" subtitle in the tutorial). I've successfully installed Blazored.LocalStorage nuget package. But I'm failing at adding "CascadingAuthenticationState" and "NotFoundContent" components to my App.razor file. The compiler can't see these componnents and asks to add using directives.

Here's the _Imports.razor:

@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.JSInterop
@using Musical_WebStore_BlazorApp.Client
@using Musical_WebStore_BlazorApp.Client.Shared
@using Microsoft.AspNetCore.Authorization
@using Blazored.LocalStorage

Here's the App.razor that's not working (and I'm asking to help me to get it working):

<CascadingAuthenticationState>
    <Router AppAssembly="typeof(Program).Assembly">
        <NotFoundContent>
            <p>Sorry, there's nothing at this address.</p>
        </NotFoundContent>
    </Router>
</CascadingAuthenticationState>

Here's the App.razor that was working (I had it by default):

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

Here are the versions of the projects:

Blazor.Client (Microsoft.AspNetCore.Blazor nuget package): 3.0.0-preview9.19465.2

ASP.NET Core (Microsoft.AspNetCore.Blazor.Server nuget package): 3.0.0-preview9.19465.2

Shared: .NET Standard 2.0 library

How do I solve this problem? It looks like these components should be available by default. And even if not, I couldnt find the namespace for these components on the internets.

like image 449
Марк Павлович Avatar asked Oct 14 '25 15:10

Марк Павлович


1 Answers

You need to install the Microsoft.AspNetCore.Components.Authorization package from NuGet. This package didn't exist at the time I wrote that article. Once you've installed it and added a using to the _Imports.razor you should be good to go.

like image 192
Chris Sainty Avatar answered Oct 18 '25 03:10

Chris Sainty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!