【问题标题】:How to get admin roles that I am a member of, from Microsoft Graph using .Net Client SDK?如何使用 .Net Client SDK 从 Microsoft Graph 获取我所属的管理员角色?
【发布时间】:2018-09-06 16:47:10
【问题描述】:

我知道如何使用下面的其余查询按类型过滤成员目录角色:

https://graph.microsoft.com/v1.0/me/memberOf/$/microsoft.graph.directoryRole

因此来自 MS Graph api 的响应仅包含 directoryRole 对象。不确定如何使用 MS Graph .Net 客户端 SDK 完成此操作,因为我们没有在实际的 Rest 请求中指定任何 OData 关键字,例如 SelectFilter

请注意,我不想只调用 memberOf 并在客户端的内存中过滤 directoryRole 类型响应,我希望此过滤器成为上面 URL 中查询的一部分,因此 memberOf 响应包含只有directoryRole s。

【问题讨论】:

    标签: c# microsoft-graph-api microsoft-graph-sdks


    【解决方案1】:

    使用 SDK,过滤是应用于对象的功能。例如:

    var users = await graphClient
        .Users
        .Request()
        .Filter("startswith(displayName,'A')")
        .GetAsync();
    

    很遗憾,这对您没有帮助,因为 /memberOf 不支持 $filter。因此,您需要在客户端上进行过滤。

    如果您正在检查特定的directoryRole,您可以从另一个方向来查看。 /members 端点确实支持按成员 id 过滤:

    v1.0/directoryRoles/{role-id}/members?$filter=id eq '{user-id}'
    

    这里需要注意的是,/members 确实支持通过userPrincipalName 进行过滤,仅支持实际的id

    【讨论】:

      【解决方案2】:

      Graph Net 客户端尚未直接支持您的要求。

      但根据我的测试,我们可以尝试以下解决方法:

      使用以下代码获取带有 DirectoryRole 的列表,然后按 DisplayName 过滤,然后检查 角色模板 id(对于 Me.MemberOf 中的 directoryRole,如果DisplayName 包含 Administrator,基本上我们是管理员角色。如果使用 DirectoryRoles api,我们可以迭代列表并检查 角色模板 id ):

      // This will contains the group too, we need to filter it to get the directoryrole
      
          IUserMemberOfCollectionWithReferencesRequest builder = graphClient.Me.MemberOf.Request();
                          IUserMemberOfCollectionWithReferencesPage page = await builder.GetAsync();
      
          // This is all directoryrole in our tenant, we need to filter by DisplayName contains **Administrator**
                      IGraphServiceDirectoryRolesCollectionRequest request = graphClient.DirectoryRoles.Request();
                      IGraphServiceDirectoryRolesCollectionPage directoryRoles = await request.GetAsync();
      

      Me.MemberOf 的结果: DirectoryRoles 的结果:

      如果解决方法仍然不能满足您的要求,我建议您在uservoicegithub issues 上提交功能请求

      补充答案: (不幸的是,Microsoft Graph 对目录资源的过滤通常非常有限。所以您现在可以使用客户端内存过滤器或提交功能请求):

      理论上,我们可以这样使用rest api(目前不支持对引用属性查询的指定过滤器。

      https://graph.microsoft.com/v1.0/me/memberOf/$/microsoft.graph.group?$filter=groupTypes/any(a:roleTemplateId eq  '62e90394-69f5-4237-9190-012177145e10')
      

      在基于 Graph Client 的 C# 代码中

      List<QueryOption> options = new List<QueryOption>
                      {
                          new QueryOption("$filter", 
                            "groupTypes/any(a:roleTemplateId eq  '62e90394-69f5-4237-9190-012177145e10'")
                      };   
      IUserMemberOfCollectionWithReferencesRequest builder = graphClient.Me.MemberOf.Request(options); 
      
                          IGraphServiceDirectoryRolesCollectionRequest request = graphClient.DirectoryRoles.Request(options);
      

      【讨论】:

      • 还有其他方法可以使用 RoleTemplateId 从 memberOf 响应中过滤特定的 directoryRole 吗?
      • 我认为 RoleTempateId 可用于过滤 MemberOf 和 DirectoryRoles 的结果(遍历列表或通过 LINQ)。 RoleTemplateId以外的方式还需要测试
      • 如果您发布一个使用 RoleTemplateId 过滤的工作示例,我将接受该答案。
      • 现在是我们的星期六,我会在星期一回办公室后做。
      • 我应该在大约 20 小时后回到我的办公室,最早在今天晚上。
      猜你喜欢
      • 1970-01-01
      • 2019-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-19
      相关资源
      最近更新 更多