我正在尝试构建一个托管在服务器上的 Blazor 应用程序,起点位于一个 razor 页面内。
类似的东西:
<component type="typeof(Main)" render-mode="ServerPrerendered" param-Data="@simple"/>
我的问题是:
OnGetAsync
中的数据库值的自定义身份验证怎么办?方法 - 我是否需要在 blazor 中重做某些内容,还是只有在 razor 页面工作时才会呈现有状态组件? 沿线的东西:
@if (HasPermission)
{
<button type="button" onclick="MutateDatabase">MutateDatabase</button>
}
最佳答案
我假设您运行 Blazor Server(在撰写本文时,WASM 仍处于预览阶段,并且在安全方面会有很大不同)。
documentataion指出 Blazor 确实与 ASP.NET Core 身份集成:
Blazor Server apps include a built-in
AuthenticationStateProvider
service that obtains authentication state data from ASP.NET Core's HttpContext.User. This is how authentication state integrates with existing ASP.NET Core server-side authentication mechanisms.
现在,回答您的问题:
[Authorize]
在 child controls differently :Only use
[Authorize]
on@page
components reached via the Blazor Router. Authorization is only performed as an aspect of routing and not for child components rendered within a page. To authorize the display of specific parts within a page, useAuthorizeView
instead.
(这似乎不是你的情况,但我会把它放在这里以防万一)
circuit
是 MS 用来标识您的应用程序实例在显示给客户端时所在的服务器切片的术语。连接是通过 websockets 维护的,并且通常限定在一个 session 中(查看您的 /_blazor
端点的 cookie 和 url 参数)。但是,不能保证用户在整个应用程序生命周期内都具有相同的电路(由于连接问题或服务器负载平衡器配置)- 没关系,您应该处理状态 persistance across circuits你自己。 <AuthorizeView>
控制渲染的内容:<AuthorizeView>
<Authorized>
<button type="button" onclick="MutateDatabase">MutateDatabase</button>
</Authorized>
<NotAuthorized>
<p>You're not signed in.</p>
</NotAuthorized>
</AuthorizeView>
从技术上讲,您可以使用
if (user.IsInRole())
声明,但那 might not get updated when User AuthenticationState changes .如果这还不够,您可以 pick up cascading
AuthenticationState
parameter或查看 implementing your own AuthenticationStateProvider
关于asp.net-core - Blazor 安全性 - Razor Pages 自定义身份验证/安全性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59784888/