AzureAD基于角色的授权

时间:2020-05-07 21:07:31

标签: c# azure azure-active-directory asp.net-core-2.1

我在AzureAdd AppRegistrations中注册了我的应用程序,并在AppRoles中为我的应用程序添加了manifest,即管理员角色。在Azure企业应用程序菜单中,我添加了一个用户,并为其分配了管理员角色,可以选择该角色。

在应用程序的Startup类中,添加了AddAuthorization部分并定义了策略RequireRole("Admin")

我将[Authorize(Roles = "Admin")]用于控制器类,并在导航栏局部视图中添加了((await AuthorizationService.AuthorizeAsync(User, "Admin")).Succeeded)

但是,当我调试时,找不到{http://schemas.microsoft.com/ws/2008/06/identity/claims/role: Admin}条目,并且User.IsInRole("Admin")返回false。用户凭据是正确的。 据我所知,我已经完成了所有步骤。我唯一能想到的是我的azure订阅不允许添加组,但是我看不到如何将其与我设置的任何内容相关联。我不使用任何群组,仅使用个人用户。 我在这里想念什么?为什么我看不到用户角色?

清单:

    {
        "allowedMemberTypes": [
            "User"
        ],
        "description": "Admin's have access to everything.",
        "displayName": "Admin",
        "id": "d1c2ade8-98f8-45fd-ba4b-6d06b947c66f",
        "isEnabled": true,
        "lang": null,
        "origin": "Application",
        "value": "Admin"
    },
"groupMembershipClaims": "SecurityGroup",

Startup.cs

services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
    .AddAzureAD(options => Configuration.Bind("AzureAd", options));

    services.AddAuthorization(options =>
    {
       options.AddPolicy("Admin", policy => policy.RequireRole("Admin"));
       options.AddPolicy("Packager", policy => policy.RequireRole("Packager"));
    });

查看:

@if ((await AuthorizationService.AuthorizeAsync(User, "Admin")).Succeeded)
{
    <li><a asp-area="" asp-controller="Home" asp-action="Admin">Admin</a></li>
}

编辑(5月8日): 使用Microsoft的this文档,它可以正常工作。将其添加到我的Startup.cs

  services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
  {
    options.Authority = options.Authority + "/v2.0/";         // Microsoft identity platform

    options.TokenValidationParameters.ValidateIssuer = false; // accept several tenants (here simplified)
  });

1 个答案:

答案 0 :(得分:1)

http://schemas.microsoft.com/ws/2008/06/identity/claims/role是旧的SAML声明类型格式。对于OIDC,Azure AD将发出一个或多个roles claims

要解决此问题,请确保在服务配置过程中使用以下代码:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

s = pd.read_csv('test.csv') #reads the .csv file
xmin = 750 #change as needed with your application
xmax = 2800 #change as needed with your application

for i in s.columns:

   plt.plot(s[i])

   axes = plt.gca(projection='polar')
   axes.set_xlim([xmin,xmax])
   plt.xlabel('Energy (eV)')
   plt.ylabel('Intensity (counts/nC)')
   plt.title('Test')
   plt.savefig("test.png", dpi = 600)
   plt.legend()
   plt.show()