60 lines
1.9 KiB
Plaintext
60 lines
1.9 KiB
Plaintext
@using Microsoft.AspNetCore.Authorization
|
|
@inject IPermissionService PermissionService
|
|
@inject IAuthorizationService AuthorizationService
|
|
|
|
<CascadingAuthenticationState>
|
|
<AuthorizeView Context="authContext">
|
|
<Authorized>
|
|
@if (_hasPermission)
|
|
{
|
|
@ChildContent
|
|
}
|
|
else if (!string.IsNullOrEmpty(NotAuthorizedContent))
|
|
{
|
|
@NotAuthorizedContent
|
|
}
|
|
</Authorized>
|
|
<NotAuthorized>
|
|
@if (!string.IsNullOrEmpty(NotAuthenticatedContent))
|
|
{
|
|
@NotAuthenticatedContent
|
|
}
|
|
</NotAuthorized>
|
|
</AuthorizeView>
|
|
</CascadingAuthenticationState>
|
|
|
|
@code {
|
|
[Parameter] public RenderFragment? ChildContent { get; set; }
|
|
[Parameter] public string? Permission { get; set; }
|
|
[Parameter] public string[]? Permissions { get; set; }
|
|
[Parameter] public bool RequireAll { get; set; }
|
|
[Parameter] public string? Policy { get; set; }
|
|
[Parameter] public string? NotAuthorizedContent { get; set; }
|
|
[Parameter] public string? NotAuthenticatedContent { get; set; }
|
|
|
|
private bool _hasPermission;
|
|
|
|
protected override async Task OnParametersSetAsync()
|
|
{
|
|
if (!string.IsNullOrEmpty(Policy))
|
|
{
|
|
var authState = await AuthorizationService.AuthorizeAsync(null, Policy);
|
|
_hasPermission = authState.Succeeded;
|
|
}
|
|
else if (!string.IsNullOrEmpty(Permission))
|
|
{
|
|
_hasPermission = await PermissionService.HasPermissionAsync(Permission);
|
|
}
|
|
else if (Permissions != null && Permissions.Length > 0)
|
|
{
|
|
if (RequireAll)
|
|
{
|
|
_hasPermission = await PermissionService.HasAllPermissionsAsync(Permissions);
|
|
}
|
|
else
|
|
{
|
|
_hasPermission = await PermissionService.HasAnyPermissionAsync(Permissions);
|
|
}
|
|
}
|
|
}
|
|
} |