我已成功设置 Blazor 应用程序以向我工作的 Azure 租户进行身份验证。身份验证工作得很好。我在 Azure 中设置了应用注册,并在 list 中定义了 appRoles。我已经将一些用户添加到应用程序中,并分配了这些角色,但是在通过身份验证后,我没有在用户上下文中获得任何角色声明。
启动文件
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => Configuration.Bind("AzureAd", options));
services.AddControllersWithViews(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
});
显现:
"appRoles": [
{
"allowedMemberTypes": [
"User"
],
"description": "Coming soon.",
"displayName": "Viewer",
"id": "{guid goes here}",
"isEnabled": true,
"lang": null,
"origin": "Application",
"value": "Viewer"
},
{
"allowedMemberTypes": [
"User"
],
"description": "Coming soon.",
"displayName": "Manager",
"id": "{guid goes here}",
"isEnabled": true,
"lang": null,
"origin": "Application",
"value": "Manager"
}
],
我试图在身份验证后使用声明检索这些角色,但没有角色通过。这显然使 IsInRole 不起作用,而且在我的一生中,我找不到任何代码示例来实现这一点。
如果有人能指出我正确的方向,我将不胜感激!
最佳答案
如果用户已被分配应用程序角色,则应在 id token 中返回。您可以使用 https://jwt.io/
解码 id token .
我没有找到 blazor 样本,但你可以引用这个 aspnetcore sample .
关于azure-active-directory - 使用 Azure AD 进行 Blazor 身份验证 - 检索用户角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60007447/