添加项目文件。
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
@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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
@inject IJSRuntime JSRuntime
|
||||
@inject ILocalizationService LocalizationService
|
||||
@inject NavigationManager Navigation
|
||||
|
||||
@* <select @bind="_selectedCulture" @onchange="OnCultureChanged">
|
||||
<option value="en-US">English</option>
|
||||
<option value="zh-CN">中文</option>
|
||||
<option value="ja-JP">日本語</option>
|
||||
</select> *@
|
||||
|
||||
@code {
|
||||
private string _selectedCulture = "en-US";
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
_selectedCulture = await JSRuntime.InvokeAsync<string>("blazorCulture.get") ?? "en-US";
|
||||
}
|
||||
|
||||
private async Task OnCultureChanged(ChangeEventArgs e)
|
||||
{
|
||||
var culture = e.Value?.ToString();
|
||||
if (!string.IsNullOrEmpty(culture))
|
||||
{
|
||||
await JSRuntime.InvokeVoidAsync("blazorCulture.set", culture);
|
||||
await LocalizationService.LoadResourcesAsync(culture);
|
||||
Navigation.NavigateTo(Navigation.Uri, forceLoad: true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
@inject ILocalizationService LocalizationService
|
||||
|
||||
@Text
|
||||
|
||||
@code {
|
||||
private string? _text;
|
||||
|
||||
[Parameter]
|
||||
public string Key { get; set; } = string.Empty;
|
||||
|
||||
[Parameter]
|
||||
public string? Culture { get; set; }
|
||||
|
||||
private string Text => _text ?? Key;
|
||||
|
||||
protected override async Task OnParametersSetAsync()
|
||||
{
|
||||
await LoadText();
|
||||
}
|
||||
|
||||
private async Task LoadText()
|
||||
{
|
||||
_text = await LocalizationService.GetStringAsync(Key, Culture) ?? Key;
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user