【问题标题】:Redirect to 403 Forbidden component in Blazor WebAssembly重定向到 Blazor WebAssembly 中的 403 禁止组件
【发布时间】:2020-08-27 08:06:32
【问题描述】:

我目前正在开发一个 .NET Standard 2.1 Blazor WebAssembly Hosted 应用程序。

在我的身份验证方案中,我使用用户角色

如果用户不是角色,即管理员,我想将所有用户重定向到<ForbiddenView />

最好在应用程序的一个地方处理这个问题。

  • 我的 App.razor 如下所示:
   <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>

你知道如何集中处理 Blazor WebAssembly 中的 403 错误吗?

您对如何处理 Blazor WebAssembly 上的 403 错误有任何建议吗?

【问题讨论】:

    标签: c# authentication blazor http-status-code-403 webassembly


    【解决方案1】:

    尝试在不同的上下文中使用 AuthorizeView:

    <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>
    

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

    如果您特别需要 NotInRole:

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

    【讨论】:

      猜你喜欢
      • 2020-08-04
      • 2017-10-06
      • 2014-05-02
      • 2014-12-01
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      相关资源
      最近更新 更多