feat: 增加未认证时不渲染页面逻辑

This commit is contained in:
Argo-Tianyi 2021-12-17 13:28:57 +08:00
parent 8b3ac481e9
commit 05cfdeefb3
3 changed files with 53 additions and 51 deletions

View File

@ -22,6 +22,9 @@
Navigation.NavigateTo($"/Admin/Index", true); Navigation.NavigateTo($"/Admin/Index", true);
} }
#else #else
/// <summary>
///
/// </summary>
protected override void OnInitialized() protected override void OnInitialized()
{ {
Navigation.NavigateTo($"/Admin/Index", true); Navigation.NavigateTo($"/Admin/Index", true);

View File

@ -1,8 +1,10 @@
@inherits LayoutComponentBase @inherits LayoutComponentBase
<Layout SideWidth="0" IsPage="true" IsFullSide="true" IsFixedHeader="true" IsFixedFooter="false" @if (Login)
{
<Layout SideWidth="0" IsPage="true" IsFullSide="true" IsFixedHeader="true" IsFixedFooter="false"
ShowFooter="true" ShowGotoTop="true" ShowCollapseBar="true" Menus="@MenuItems" ShowFooter="true" ShowGotoTop="true" ShowCollapseBar="true" Menus="@MenuItems"
OnAuthorizing = "@OnAuthorizing" OnAuthorizing="@OnAuthorizing"
UseTabSet="true" TabDefaultUrl="/Admin/Index"> UseTabSet="true" TabDefaultUrl="/Admin/Index">
<Header> <Header>
<span class="ms-3 flex-fill">Bootstrap of Blazor</span> <span class="ms-3 flex-fill">Bootstrap of Blazor</span>
@ -32,6 +34,7 @@
<div class="flex-fill text-center text-dark">@Footer</div> <div class="flex-fill text-center text-dark">@Footer</div>
</Footer> </Footer>
<NotAuthorized> <NotAuthorized>
<RedirectToLogin /> <Redirect />
</NotAuthorized> </NotAuthorized>
</Layout> </Layout>
}

View File

@ -33,10 +33,6 @@ namespace BootstrapAdmin.Web.Shared
[NotNull] [NotNull]
private BootstrapAppContext? Context { get; set; } private BootstrapAppContext? Context { get; set; }
[Inject]
[NotNull]
private NavigationManager? Navigation { get; set; }
[Inject] [Inject]
[NotNull] [NotNull]
private IBootstrapAdminService? SecurityService { get; set; } private IBootstrapAdminService? SecurityService { get; set; }
@ -47,30 +43,30 @@ namespace BootstrapAdmin.Web.Shared
private string? DisplayName { get; set; } private string? DisplayName { get; set; }
/// <summary> private bool Login { get; set; }
///
/// </summary>
protected override void OnInitialized()
{
base.OnInitialized();
MenuItems = NavigationsService.GetAllMenus("Admin").ToAdminMenus();
Title = DictsService.GetWebTitle();
Footer = DictsService.GetWebFooter();
}
/// <summary> /// <summary>
/// /// OnInitializedAsync 方法
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
var state = await AuthenticationStateProvider.GetAuthenticationStateAsync(); var state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var userName = state.User.Identity?.Name; var userName = state.User.Identity?.Name;
if (!string.IsNullOrEmpty(userName))
{
DisplayName = UsersService.GetDisplayName(userName); DisplayName = UsersService.GetDisplayName(userName);
Context.UserName = userName; Context.UserName = userName;
Context.DisplayName = DisplayName; Context.DisplayName = DisplayName;
MenuItems = NavigationsService.GetAllMenus("Admin").ToAdminMenus();
}
Title = DictsService.GetWebTitle();
Footer = DictsService.GetWebFooter();
Login = true;
} }
private Task<bool> OnAuthorizing(string url) => SecurityService.AuhorizingNavigation(Context.UserName, url); private Task<bool> OnAuthorizing(string url) => SecurityService.AuhorizingNavigation(Context.UserName, url);