Search code examples
c#authenticationblazorhttp-status-code-403webassembly

Redirect to 403 Forbidden component in Blazor WebAssembly


I'm currently working on a .NET Standard 2.1 Blazor WebAssembly Hosted application.

In my authentication scheme I use User Roles.

I want to redirect all users to a <ForbiddenView /> if a user is not in a role i.e. Admin.

It would be nice to handle this on one place in the application.

  • My App.razor looks like this:
   <CascadingAuthenticationState>
        <Router AppAssembly="@typeof(Program).Assembly">
            <Found Context="routeData">
                <AuthorizeRouteView RouteData="@routeData">
                    <Authorizing>
                        <p>Authorizing...</p>
                    </Authorizing>
                    <NotAuthorized>
                        @if (!context.User.Identity.IsAuthenticated)
                        {
                           // User is not authenticated - forward to login view
                            <LoginView />
                        }
                        else if(context.User.Identity.IsAuthenticated && context.User.Identity.NotInRole) // .NotInRole does not exist!!
                        {
                            // 403 - User is authenticated, but not in a specific role i.e. admin to view a page in my app. The server understood the request, but is refusing to fulfill it.
                            <ForbiddenView />
                        }
                        else
                        {
                           // 401 - Login of my user happend - the request already includes Authorization credentials.
                            <NotAuthorizedView />
                        }
                    </NotAuthorized>
                </AuthorizeRouteView>
            </Found>
            <NotFound>
                <NotFoundView />
            </NotFound>
        </Router>
    </CascadingAuthenticationState>

Do you know how to handle the 403 error in Blazor WebAssembly in a centralized way?

Do you have any suggests on how to handle the 403 error on Blazor WebAssembly?


Solution

  • Try using AuthorizeView with a different context :

    <CascadingAuthenticationState>
        <Router AppAssembly="@typeof(Program).Assembly">
            <Found Context="routeData">
                <AuthorizeRouteView RouteData="@routeData">
                    <Authorizing>
                        <p>Authorizing...</p>
                    </Authorizing>
                    <NotAuthorized>
                        <AuthorizeView Context="authenticated">
                            <Authorized Context="authenticated">
                                <AuthorizeView Roles="WhatEver" Context="role">
                                    <Authorized Context="role">
                                        <NotAuthorizedView />
                                    </Authorized>
                                    <NotAuthorized Context="role">
                                        <ForbiddenView />
                                    </NotAuthorized>
                                </AuthorizeView>
                            </Authorized>
                            <NotAuthorized Context="authenticated">
                                <RedirectToLogin />
                            </NotAuthorized>
                        </AuthorizeView>
                    </NotAuthorized>
                </AuthorizeRouteView>
            </Found>
            <NotFound>
                <NotFoundView />
            </NotFound>
        </Router>
    </CascadingAuthenticationState>
    

    or !context.User.IsInRole("WhatEver");

    if you specifically need NotInRole:

    public static bool NotInRole(this ClaimsPrincipal claimsPrincipal) 
        => claimsPrincipal.Claims.All(c => c.Type != "role");