Blazor AuthorizeView:如何使用自定义授权过滤器

时间:2020-02-14 00:47:33

标签: c# razor authorization blazor blazor-boilerplate

我正在使用Blazor Boilerplate中的以下代码:(BlazorBoilerPlate GitHub

@using Microsoft.AspNetCore.Components
@inject NavigationManager navigationManager
@inject AuthenticationStateProvider authStateProvider

<AuthorizeView Context="AuthorizeContext">
<Authorized>
    <MatTooltip Tooltip="Logout">
        <MatIconButton Icon="exit_to_app" RefBack="@context" OnClick="LogoutClick"></MatIconButton>
    </MatTooltip>
</Authorized>
<NotAuthorized>
    <MatTooltip Tooltip="Login">
        <MatIconButton Icon="account_box" RefBack="@context" Link="/account/login"></MatIconButton>
    </MatTooltip>
</NotAuthorized>

@code {
     async Task LogoutClick()
     {
          await ((IdentityAuthenticationStateProvider)authStateProvider).Logout();
     navigationManager.NavigateTo("/account/login");
     }
}

“ AuthorizeContext”来自哪里?脱脂注射?我如何在这里使用自己的自定义授权过滤器。

只需查找有关此特定论点的任何猜测或信息。

这是我自定义的AuthorizationFilter,我不知道如何在blazor剃须刀文件中使用它。

namespace BlazorBoilerplate.Shared.AuthorizationDefinitions

{ 公共类OfficeAuthorize:IAuthorizationFilter { 私有只读字符串_scenario;

    public OfficeAuthorize(string scenario)
    {
        _scenario = scenario;
    }

    public void OnAuthorization(AuthorizationFilterContext context)
    {
        //context.HttpContext.Request.Query
        //custom validation rule
        if (_scenario == "OfficeAssignment")
        {
            context.Result = new ForbidResult();
        }
    }
}

public class OfficeAuthorizeAttribute : TypeFilterAttribute
{
    public OfficeAuthorizeAttribute(int input) : base(typeof(OfficeAuthorize))
    {
        Arguments = new object[] { input };
    }
}

}

0 个答案:

没有答案