首先是在 Microsoft Entra admin center 中配置你的应用程序角色
然后分配用户到你创建的角色
1. 首先从下图找到你要配置的程序
2. 然后找到分配用户到角色的地方
选择用户
再选择角色
这样就成功给用户分配了权限
接下来就可以在Blazor页面中读取了
@using Microsoft.AspNetCore.Components.Authorization
@inject AuthenticationStateProvider AuthenticationStateProvider
@code {
private ClaimsPrincipal user;
protected override async Task OnInitializedAsync()
{
LoadFromName();
NavigationManager.LocationChanged += OnLocationChanged;
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
user = authState.User;
var claims = user.Claims.Where(c => c.Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/role").ToList();
var IsUser = claims.Any(claim => claim.Value.ToLower() == "user");
var IsSuperUser = claims.Any(claim => claim.Value.ToLower() == "superuser");
}
}