.net core blazor基于角色的列

.net core blazor基于角色的列,.net-core,authorization,blazor,.net Core,Authorization,Blazor,我有基于Blazor服务器的应用程序。我基本上在应用程序中为管理员显示数据库列。在我添加的startup.cs中,使用Azure AD完成身份验证 options.AddPolicy("AppAdmins", policy => policy.RequireRole("Admin", "SuperAdmin", "Owner")); 在主页上我有 <AuthorizeView Policy="

我有基于Blazor服务器的应用程序。我基本上在应用程序中为管理员显示数据库列。在我添加的startup.cs中,使用Azure AD完成身份验证

options.AddPolicy("AppAdmins", policy => policy.RequireRole("Admin", "SuperAdmin", "Owner"));
在主页上我有

<AuthorizeView Policy="AppAdmins">
    <Authorized>
        @Body
    </Authorized>
    <NotAuthorized>
        You are not authorized to use this application.
    </NotAuthorized>
</AuthorizeView>

@身体
您无权使用此应用程序。
这是可行的,现在对于特定的角色用户,我需要显示特定于角色的db列。如何访问Index.razor或Index.razor.cs中的角色以筛选字段


我是Blazor和.Net Core的新手。

您可以在组件中插入AuthenticationStateProvider,获取 用户,检查他是否经过身份验证,然后检索分配给他的角色

@using System.Security.Claims
@using Microsoft.AspNetCore.Components.Authorization
@inject AuthenticationStateProvider AuthenticationStateProvider


 var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
        var user = authState.User;

        if (user.Identity.IsAuthenticated)
        {
            var identity = (ClaimsIdentity)user.Identity;
            var roleClaims = identity.FindAll(identity.RoleClaimType);

           foreach (var existingClaim in roleClaims)
           {
             // Do something with the roles...
            }
        }
        else
        {
           // "The user is NOT authenticated.";
        }

如果有人遇到这种情况,您可以查看。我花了两天的时间才弄明白它是怎么做到的,所有的论坛文章我都要看,微软的文档太多了。