因此,我通过遵循Microsoft文档开始了事情:
Secure an ASP.NET Core Blazor WebAssembly hosted app with Azure Active Directory
Azure AD Groups, Administrative Roles, and user-defined roles
Azure方面似乎设置得很好:
这很好:
@page "/clients"
@inject NavigationManager navigationManager
@inject HttpClient Http
@inject AppData appData
@inject AuthenticationStateProvider AuthenticationStateProvider
@attribute [Authorize]
我已经打印了声明以查看发生了什么事情
protected async override Task OnInitializedAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
foreach (var claim in user.Claims)
{
System.Diagnostics.Debug.WriteLine(claim.Type + ":" + claim.ValueType + ":" + claim.Value);
}
}
这是打印的行之一:
roles:http://www.w3.org/2001/XMLSchema#string:["Admin"]
所以我可以看到我在Azure上的应用清单中添加的appRole到了这里。 (下面的GUID出于隐私目的而隐藏)
"appRoles": [
{
"allowedMemberTypes": [
"User"
],
"description": "Can view everything.",
"displayName": "Global Viewer",
"id": "IDGOESHERE",
"isEnabled": true,
"lang": null,
"origin": "Application",
"value": "GlobalViewer"
},
{
"allowedMemberTypes": [
"User"
],
"description": "Admins can access restricted areas.",
"displayName": "Admin",
"id": "IDGOESHERE",
"isEnabled": true,
"lang": null,
"origin": "Application",
"value": "Admin"
}
],
还将我的用户添加到企业应用程序的管理员角色。
但是,在[Authorize]属性指令中添加角色会使我无法访问页面:(您无权访问此资源。)
attribute [Authorize(Roles = "Admin")]
这在Program.cs中(我在“ GUIDGOESHERE”中有实际的GUID)
builder.Services.AddMsalAuthentication(options =>
{
builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
options.ProviderOptions.DefaultAccessTokenScopes.Add("GUIDGOESHERE/EmployeesAccess");
options.ProviderOptions.DefaultAccessTokenScopes.Add("GUIDGOESHERE/AdminAccess");
options.UserOptions.RoleClaim = "roles";
});
问题可能出在我的角色声明中。也许问题在于此声明看起来像数组吗?如果是这样,我该如何解决?
答案 0 :(得分:1)
结果证明Azure可能比ASP.NET Core领先
Azure AD身份验证默认模板无法立即使用,需要进行一些调整。
在此处遵循MS文档中的步骤:Azure AD Groups, Administrative Roles, and user-defined roles
长话短说: