Search code examples
c#razorblazor-webassembly

Check admin authorization with AuthorizeView from AuthenticationStateProvider


I'm using the AuthenticationStateProvider in my Blazor WASM project and I figured out how to show content for logged in users but how can I show content for users who are logged in AND have admin privileges? My backend API send the "Admin" claim in the JWT token but I can't figure out how to read it using the AuthorizeView component.

<AuthorizeView>
    <Authorized>
        <MudButton OnClick="Logout">Logout</MudButton>
    </Authorized>
</AuthorizeView>

Solution

  • You can specify roles in the AuthorizeView component as parameters:

    <AuthorizeView Roles="Admin">
        <Authorized>
            <MudButton OnClick="Logout">Logout</MudButton>
        </Authorized> 
    </AuthorizeView>
    

    Documentation here: https://learn.microsoft.com/en-us/aspnet/core/blazor/security/?view=aspnetcore-7.0#role-based-and-policy-based-authorization