admin管理员组

文章数量:1296843

I have a Blazor Web App and I'm am trying to add authentication. I can verify that my user is logged in by calling the following code on one of my unsecured pages:

 private async Task CheckAuth()
    {
        var state = await AuthStateProvider.GetAuthenticationStateAsync();
        var user = state.User;

        if (user.Identity.IsAuthenticated)
        {
            Console.WriteLine("User is authenticated!");
        }
        else
        {
            Console.WriteLine("User is NOT authenticated.");
        }
    }

But, as soon as I try to visit the following page with @attribute [Authorized], to shows that my user is NOT logged in:

@using Aelios.Client.Layout
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization

@page "/secure/dashboard"
@attribute [Authorize]
@layout AdminLayout


<PageTitle>Dashboard</PageTitle>

INSIDE SECURE DASHBOARD


@code {

}

If I however REMOVE the attribute and try the following, it shows that the user indeed IS authenticated:


@using Aelios.Client.Layout
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization

@page "/secure/dashboard"
@layout AdminLayout


<PageTitle>Dashboard</PageTitle>

INSIDE SECURE DASHBOARD

<CascadingAuthenticationState>
    <AuthorizeView>
        <Authorized>
            <p>✅ You are authorized!</p>
            <p>

本文标签: authenticationattribute Authorized not recognizing authenticated userStack Overflow