Skip to content

使用 ReuseTabs 组件时,通过级联传入 RouteData 的方式会导致 AuthorizeRouteView 失效。 #203

@jzwo

Description

@jzwo

操作步骤:

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()));
    }
}

  1. 在MyAntDesignAppSimple .Program.cs中添加以下内容
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddScoped<AuthenticationStateProvider, PersistingRevalidatingAuthenticationStateProvider>();
  1. 添加一个需要权限的页面
@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++;
    }
}
  1. 将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 />

结果依旧可以导航到该页面

image

后续

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions