我正在使用自定义策略来保护服务器端 Blazor 应用程序中的页面。一切都运行良好,除了我的策略之一需要了解请求的查询参数。例如,URI 路径类似于https://mywebsite/profile/1234,用于查看/编辑 id=1234 的配置文件。显然我们只希望 profileId = 1234 的用户编辑此页面。我如何在我的 中检查这一点IAuthorizationHandler?
我尝试注入HttpContext并读取 request.Query 项目,但它总是“/”或“/_blazor”,因为它是 SPA 课程。我尝试注入NavigationManager(以前UriHelper)从那里获取 URI,但收到错误:
'RemoteNavigationManager' has not been initialized.
Run Code Online (Sandbox Code Playgroud)
我还尝试使用 Resource 参数将信息传递到我的处理程序中。我找不到任何如何执行此操作的示例,所以这是我的尝试:
这是我的 profile.razor 代码,我使用Policy="CanEditProfile"限制访问
@inject NavigationManager NavigationManager
<AuthorizeView Policy="CanEditProfile">
<NotAuthorized>
<h2 class="mt-5">You are not authorized to view this page</h2>
</NotAuthorized>
<Authorized>
<div class="container my-profile">
<h2>My Profile</h2>
Run Code Online (Sandbox Code Playgroud)
我的IAuthorizationHandler代码:
public Task HandleAsync(AuthorizationHandlerContext context)
{
if (context == null || httpContextAccessor.HttpContext == null) return Task.CompletedTask;
// …Run Code Online (Sandbox Code Playgroud) authorization blazor policy-based-security blazor-server-side