asp.net-core - AuthorizeRouteView 不适用于已注销的用户

标签 asp.net-core blazor blazor-server-side blazor-client-side

我们开始将一个项目迁移到 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,甚至在私有(private)浏览器窗口中也尝试过。

我在另一个组件中注入(inject)了 HTTPContext 以查看当前用户发生了什么。当前用户不为空,它有一个身份,但显示为未经身份验证,并且没有预期的声明。

我在这里遗漏了什么吗?

最佳答案

我在 WebAssembly(客户端)项目中对当前 Blazor 版本有同样的问题。

如果 Blazor 团队能告诉我们我们做错了什么,那就太好了,因为您使用的代码正是他们建议使用的代码。

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

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

关于asp.net-core - AuthorizeRouteView 不适用于已注销的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58789545/

相关文章:

asp.net-mvc - 如何在Asp.net Core中将缓存更新为并发请求?

blazor - Blazor 中的 StateHasChanged() 与 InvokeAsync(StateHasChanged)

c# - 将枚举的每个值绑定(bind)到复选框 Blazor

blazor-server-side - 如何获取 Blazor 客户端浏览器选择的语言? (这样我就可以在该客户端浏览器上使用正确的小数点分隔符进行计算)

javascript - Blazor fire onchange event when Chosen drop down value changes

c# - 在 .net core 中编写 windows 服务

azure - 如何修复 "Microsoft.Azure.KeyVault.Models.KeyVaultErrorException: ' 操作返回无效状态代码 'NotFound' '"

asp.net - dotnet 发布和 web.config?

.net - Blazor BaseURI http 与 https 问题

c# - 重命名 Blazor 组件文件名时出现错误