我们开始将一个项目迁移到 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/