Blazor应用程序中是否实现了拒绝访问的路径

时间:2019-12-12 08:19:20

标签: asp.net-core authorization blazor blazor-server-side

Blazor应用程序中是否内置AccessDeniedPath功能。 我的应用对某些组件使用基于角色的授权方法。例如(@attribute [Authorize(Roles ="Admin")]
在我的App.razor中

<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
    <NotAuthorized>
            <h1>Sorry</h1>
            <p>You're not authorized to reach this page.</p>
            <p>You may need to log in as a different user.</p>
    </NotAuthorized>
    <Authorizing>
        <h1>Authentication in progress</h1>
        <p>Only visible while authentication is in progress.</p>
    </Authorizing>
</AuthorizeRouteView>

属于NotAuthorized的请求,无论是来自无权限用户还是匿名用户(完全未经授权)。

1 个答案:

答案 0 :(得分:2)

有一个context可用于确定当前用户是否已通过身份验证。

例如:

<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" >
    <NotAuthorized>
        @if(!context.User.Identity.IsAuthenticated)
        {
            <p> You need Login! </p>  
        } else{
            <p> Sorry, the content here is not for you! </p> 
            @*or render or component like <AccessDenied /> directly *@
        }
    </NotAuthorized>
    <Authorizing>
        <h1>Authentication in progress</h1>
        <p>Only visible while authentication is in progress.</p>
    </Authorizing>
</AuthorizeRouteView>