AuthorizeRouteView不适用于注销用户

我们开始将项目迁移到blazor,并且在将内容限制为仅已登录的用户方面存在一些问题。

对于此测试,我们使用了标准的Visual Studio 2019 Blazor服务器端项目模板,其中启用了本地用户和帐户身份验证。

然后我们将App.razor文件更改为以下内容:

<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
    <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(Mainlayout)">
        <NotAuthorized>
            NOT AUTHORIZED!!!!!
        </NotAuthorized>
        <Authorizing>
            AUTHORIZING!!!!!
        </Authorizing>
        </AuthorizeRouteView>
</Found>
<NotFound>
    <CascadingAuthenticationState>
        <LayoutView Layout="@typeof(Mainlayout)">
            <p>Sorry,there's nothing at this address.</p>
        </LayoutView>
    </CascadingAuthenticationState>
</NotFound>

<NotAuthorized>中的代码永远不会被击中,我已经确认浏览器上没有cookie,甚至还尝试在专用浏览器窗口中尝试

我在另一个组件中注入了HTTPContext,以查看当前用户的情况。当前用户不为null,具有身份,但显示未经身份验证,并且没有预期的声明。

我这里缺少什么吗?

chenglong676 回答:AuthorizeRouteView不适用于注销用户

在WebAssembly(客户端)项目中,当前的Blazor版本存在相同的问题。

如果Blazor团队可以告诉我们我们做错了什么,那太好了,因为您完全使用了他们建议使用的代码。

我在MainLayout中使用<AuthorizeView>找到了解决方法。 <NotAuthorized>部分已正确执行,您可以使用他们建议的to force a redirect to log on代码:

<AuthorizeView>
    <Authorized>
        <!-- Markup of your MainLayout -->
    </Authorized>
    <NotAuthorized>
        <RedirectToLogin />
    </NotAuthorized>
</AuthorizeView>
,

要使 AuthorizeRouteView 工作,需要明确哪些页面/组件需要授权。您可以使用授权属性:

@using Microsoft.AspNetCore.Authorization
@page "/counter"
@attribute [Authorize]
本文链接:https://www.f2er.com/3128901.html

大家都在问