Gre*_*mps 6 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>
Run Code Online (Sandbox Code Playgroud)
中的代码<NotAuthorized>永远不会被击中,我已经确认浏览器上没有cookie,甚至也在私人浏览器窗口中尝试过。
我在另一个组件中注入了 HTTPContext 来查看当前用户发生了什么。当前用户不为空,它具有身份,但显示未经身份验证,并且没有预期的声明。
我在这里缺少什么吗?
为了使 AuthorizeRouteView 工作,需要明确哪些页面/组件需要授权。您可以使用授权属性:
@using Microsoft.AspNetCore.Authorization
@page "/counter"
@attribute [Authorize]
Run Code Online (Sandbox Code Playgroud)
我在 WebAssembly(客户端)项目中的当前 Blazor 版本也遇到同样的问题。
如果 Blazor 团队能够告诉我们做错了什么,那就太好了,因为您完全使用了他们建议使用的代码。
<AuthorizeView>我通过在 MainLayout 中使用 an 找到了解决方法。该<NotAuthorized>部分已正确执行,您可以使用他们建议的代码强制重定向登录:
<AuthorizeView>
<Authorized>
<!-- Markup of your MainLayout -->
</Authorized>
<NotAuthorized>
<RedirectToLogin />
</NotAuthorized>
</AuthorizeView>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3075 次 |
| 最近记录: |