!88 feat(#I3S4R1): 增加未授权自动跳转功能

* feat: 增加未授权自动跳转功能
* refactor: 重构代码
This commit is contained in:
Argo 2021-05-20 12:26:53 +08:00
parent 4b39a2da63
commit 361037cdbd
6 changed files with 22 additions and 15 deletions

View File

@ -1,5 +1,5 @@
<CascadingAuthenticationState> <CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly" AdditionalAssemblies="new[] { typeof(MainLayout).Assembly }" PreferExactMatches="@true"> <Router AppAssembly="@typeof(MainLayout).Assembly" PreferExactMatches="@true">
<Found Context="routeData"> <Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"> <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized> <NotAuthorized>

View File

@ -5,7 +5,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="BootstrapBlazor" Version="5.0.30" /> <PackageReference Include="BootstrapBlazor" Version="5.0.31-beta03" />
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="5.0.6" /> <PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="5.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="5.0.6" /> <PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="5.0.6" />
</ItemGroup> </ItemGroup>

View File

@ -1,4 +1,5 @@
@page "/Home/Index" @page "/"
@page "/Home/Index"
<h1>Hello, world!</h1> <h1>Hello, world!</h1>

View File

@ -2,7 +2,6 @@
using BootstrapBlazor.Components; using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Routing;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
@ -30,6 +29,8 @@ namespace Bootstrap.Client.Blazor.Shared.Shared
private bool ShowFooter { get; set; } = true; private bool ShowFooter { get; set; } = true;
private bool IsAuthenticated { get; set; }
[NotNull] [NotNull]
private List<MenuItem>? Menus { get; set; } private List<MenuItem>? Menus { get; set; }
@ -82,7 +83,6 @@ namespace Bootstrap.Client.Blazor.Shared.Shared
{ {
UserName = state.User.Identity.Name; UserName = state.User.Identity.Name;
DisplayName = UserHelper.RetrieveUserByUserName(UserName)?.DisplayName; DisplayName = UserHelper.RetrieveUserByUserName(UserName)?.DisplayName;
}
// 模拟异步线程切换,正式代码中删除此行代码 // 模拟异步线程切换,正式代码中删除此行代码
await Task.Yield(); await Task.Yield();
@ -92,8 +92,11 @@ namespace Bootstrap.Client.Blazor.Shared.Shared
{ {
[""] = "Index" [""] = "Index"
}; };
// 获取登录用户菜单
Menus = GetMenus(); Menus = GetMenus();
} }
}
private List<MenuItem> GetMenus() private List<MenuItem> GetMenus()
{ {

View File

@ -1,6 +1,8 @@
@using BootstrapBlazor.Components @using Bootstrap.Client.Blazor.Shared.Shared
@using BootstrapBlazor.Components
@using Microsoft.AspNetCore.Authorization @using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms @using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing @using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web @using Microsoft.AspNetCore.Components.Web

View File

@ -1,4 +1,5 @@
@page "/" @page "/"
@using Bootstrap.Client.Blazor.Shared
@namespace Bootstrap.Client.Blazor.Pages @namespace Bootstrap.Client.Blazor.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{ @{