操作步骤:
1.使用 dotnet new antdesign -o MyAntDesignAppSimple 创建模板
2.添加文件 PersistingRevalidatingAuthenticationStateProvider
internal sealed class PersistingRevalidatingAuthenticationStateProvider : AuthenticationStateProvider
{
public override Task<AuthenticationState> GetAuthenticationStateAsync()
{
//var cI = new ClaimsIdentity("test");
//cI.AddClaim(new Claim(ClaimTypes.Name, "admin"));
//cI.AddClaim(new Claim(ClaimTypes.Email, "admin@test"));
//var cP = new ClaimsPrincipal(cI);
//var state = new AuthenticationState(cP);
//var task = Task.FromResult(state);
//NotifyAuthenticationStateChanged(task);
//return task;
return Task.FromResult(new AuthenticationState(new ClaimsPrincipal()));
}
}
- 在MyAntDesignAppSimple .Program.cs中添加以下内容
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddScoped<AuthenticationStateProvider, PersistingRevalidatingAuthenticationStateProvider>();
- 添加一个需要权限的页面
@page "/counter"
@using Microsoft.AspNetCore.Authorization
@attribute [Authorize]
<PageTitle>Counter</PageTitle>
<h1>Counter</h1>
<AuthorizeView>
<NotAuthorized>没权限</NotAuthorized>
<Authorized>有权限Hello @context.User.Identity?.IsAuthenticated.ToString();</Authorized>
</AuthorizeView>
<p role="status">Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
- 将Routes文件替换为以下内容
<Router AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
<CascadingValue Value="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(Layouts.BasicLayout)">
<NotAuthorized>
没权限路由
</NotAuthorized>
</AuthorizeRouteView>
</CascadingValue>
<FocusOnNavigate RouteData="routeData" Selector="h1" />
</Found>
</Router>
<AntContainer />
结果依旧可以导航到该页面

后续
注释包裹AuthorizeRouteView 的CascadingValue,并向 ReuseTabs 组件传入 Body 参数后恢复正常

操作步骤:
1.使用 dotnet new antdesign -o MyAntDesignAppSimple 创建模板
2.添加文件 PersistingRevalidatingAuthenticationStateProvider
结果依旧可以导航到该页面
后续
注释包裹AuthorizeRouteView 的CascadingValue,并向 ReuseTabs 组件传入 Body 参数后恢复正常
