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

标签 office365 microsoft-graph-api azure-ad-graph-api

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

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 token 数据。

{
  "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 token 数据。

{
  "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 的API。 但是,Azure 管理门户(在您的开发租户内)也会创建关联的应用实例 (servicePrincipal) 并记录同意。这是您在代码中缺少的最后一部分,也是应用程序角色未显示在 token 中的原因。

您需要做的是将 ActivityFeed.Read 角色分配给您的 servicePrincipal。这可以通过 https://graph.windows.net/ 上的 POST 来完成/servicePrincipals//appRoleAssignments 或通过您似乎正在使用的 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 调用现在应该可以按需要工作。

希望这有帮助,

关于office365 - 通过代码新创建的 Azure AD 应用程序不会生成具有所需角色的 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35407351/

相关文章:

Azure B2C : Webhook Notifications of User Activity Aren't Being Picked Up

ios - 针对 iOS 上的 Azure Graph API 的非交互式登录(缓存凭据)

azure - 如何在 SPA 应用程序中从 Web API 访问 Graph API

c# - 尝试使用 Microsoft Graph API v1.0 查询用户的 Office365 个人资料照片时获取 "ErrorAccessDenied"

email - Jenkins 和 Office365 电子邮件通知设置

azure - 创建新的 AzureAD 用户并添加到通讯组列表

microsoft-graph-api - Microsoft Graph API - 权限不足,无法完成操作

Excel 365 - 搜索表为每行返回 true

c# - 通过 Microsoft Graph 邀请用户时出现多项选择错误

microsoft-graph-api - 如何使用 MS Graph API 执行可恢复的上传到 SharePoint 站点(非根)子文件夹