2

我们正在尝试使用 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"
}

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

4

1 回答 1

2

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

当您通过 Azure 管理门户时,它会创建一个应用程序对象,允许您设置应用程序所需的权限(这通常会推动同意体验)。这类似于您为创建应用程序对象和设置所需资源访问而调用的 API。 但是,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 调用现在应该可以正常工作了。

希望这可以帮助,

于 2016-02-21T02:35:36.493 回答