通过代码新创建的 Azure AD 应用程序不会生成具有所需角色的令牌

Newly created Azure AD Application through code does not generate token with required roles

我们正在尝试使用 Microsoft Graph 客户端创建具有 "ActivityFeed.Read" 权限的 Azure AD 应用程序。下面的示例成功创建了应用程序,但是从该应用程序生成的令牌不包含角色 "ActivityFeed.Read"。如果我们转到 Azure 门户并对新创建的应用程序进行任何简单更改并手动保存并等待一分钟,则生成的令牌具有所需的角色。

public static void AddApplication()
    {
        ActiveDirectoryClient activeDirectoryClient = AuthenticationHelper.GetActiveDirectoryClientAsUser();
        Application appObject = new Application { DisplayName = "MyNewTest" };
        appObject.IdentifierUris.Add("https://localhost/MyNewTest/" + Guid.NewGuid());
        appObject.ReplyUrls.Add("https://localhost/MyNewTest");
        appObject.Homepage = "https://localhost/MyNewTest/home";

        // Add Office 365 Management APIs 
        RequiredResourceAccess app1 = new RequiredResourceAccess();
        app1.ResourceAppId = "c5393580-f805-4401-95e8-94b7a6ef2fc2";
        //ActivityFeed.Read Role
        app1.ResourceAccess.Add(new ResourceAccess() { Id = Guid.Parse("594c1fb6-4f81-4475-ae41-0c394909246c"), Type = "Role" });
        appObject.RequiredResourceAccess.Add(app1);

        PasswordCredential passWordCredential = new PasswordCredential
        {
            StartDate = DateTime.UtcNow,
            EndDate = DateTime.UtcNow.AddYears(1),
            Value = "xxxxxxxxxx"
        };
        appObject.PasswordCredentials.Add(passWordCredential);
        activeDirectoryClient.Applications.AddApplicationAsync(appObject).Wait();
        ServicePrincipal newServicePrincpal = new ServicePrincipal();
        if (appObject != null)
        {
            newServicePrincpal.DisplayName = appObject.DisplayName;
            newServicePrincpal.AccountEnabled = true;
            newServicePrincpal.AppId = appObject.AppId;
            activeDirectoryClient.ServicePrincipals.AddServicePrincipalAsync(newServicePrincpal).Wait();
        }
    }

下面是创建新应用程序后立即用于 oauth2 身份验证的解码 jwt 令牌数据。

{
  "aud": "https://manage.office.com",
  "iss": "https://sts.windows.net/de473ccc-dbc5-4625-8006-11e0e3ea8b7d/",
  "iat": 1455531167,
  "nbf": 1455531167,
  "exp": 1455535067,
  "appid": "71da9ffb-b583-43c4-bb7a-9c6e1fe30624",
  "appidacr": "1",
  "idp": "https://sts.windows.net/de473ccc-dbc5-4625-8006-11e0e3ea8b7d/",
  "oid": "36a47844-98e8-44d5-b69e-cf114772d1d3",
  "sub": "36a47844-98e8-44d5-b69e-cf114772d1d3",
  "tid": "de473ccc-dbc5-4625-8006-11e0e3ea8b7d",
  "ver": "1.0"
}

以下是经过我们手动更改并保存后用于 oauth2 身份验证的解码 jwt 令牌数据。

{
  "aud": "https://manage.office.com",
  "iss": "https://sts.windows.net/de473ccc-dbc5-4625-8006-11e0e3ea8b7d/",
  "iat": 1455531317,
  "nbf": 1455531317,
  "exp": 1455535217,
  "appid": "71da9ffb-b583-43c4-bb7a-9c6e1fe30624",
  "appidacr": "1",
  "idp": "https://sts.windows.net/de473ccc-dbc5-4625-8006-11e0e3ea8b7d/",
  "oid": "36a47844-98e8-44d5-b69e-cf114772d1d3",
  "roles": [
    "ActivityFeed.Read"
  ],
  "sub": "36a47844-98e8-44d5-b69e-cf114772d1d3",
  "tid": "de473ccc-dbc5-4625-8006-11e0e3ea8b7d",
  "ver": "1.0"
}

请让我们知道如何以编程方式创建具有所需角色的应用程序。

这个操作也可以通过Microsoft Graph来完成,但我是基于AAD Graph来回答的,因为AAD Graph有一个客户端库,你似乎正在使用它.

当您浏览 Azure 管理门户时,它会创建一个应用程序对象,允许您设置该应用程序 需要 的权限(这通常会推动同意体验)。这类似于您调用以创建应用程序对象并设置 RequiredResourceAccess 的 APIs。 但是,Azure 管理门户还(在您的开发租户内)创建关联的应用程序实例 (servicePrincipal) 并记录许可。这是您的代码中缺少的最后一部分,这就是应用程序角色未显示在令牌中的原因。

您需要做的是将 ActivityFeed.Read 角色分配给您的 servicePrincipal。这可以通过 https://graph.windows.net//servicePrincipals//appRoleAssignments 上的 POST 或通过 AAD 图形客户端库来完成,因为您似乎正在使用它。以下也应该工作。 (请注意,在 2016 年 3 月 15 日之前,我们遇到了一个错误,导致此操作无法成功。)

// create the app role assignment
AppRoleAssignment appRoleAssignment = new AppRoleAssignment();
appRoleAssignment.Id = appRole.Id;  // id for ActivityFeed.Read
appRoleAssignment.ResourceId = resourceId; //id for the resource
appRoleAssignment.PrincipalType = "ServicePrincipal";
appRoleAssignment.PrincipalId = Guid.Parse(newServicePrincipal.ObjectId);
newServicePrincipal.AppRoleAssignments.Add(appRoleAssignment);

// assign the app role
await newServicePrincipal.UpdateAsync();

更新:上述错误现已修复。代码和 REST API 调用现在应该可以正常工作了。

希望这对您有所帮助,