代码之家  ›  专栏  ›  技术社区  ›  Dogu Arslan

如何使用.Net客户端SDK从Microsoft Graph获取我所属的管理员角色?

  •  1
  • Dogu Arslan  · 技术社区  · 6 年前

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

    directoryRole 物体。不确定如何使用MS-Graph.Net客户端SDK完成这项工作,因为我们没有指定任何类似OData的关键字 Select Filter

    请注意,我不想打电话只是 memberOf 和过滤器 目录角色 在客户端的内存中键入响应,我希望这个过滤器成为查询的一部分,就像上面的URL一样,所以 响应仅包含 目录角色 s。

    2 回复  |  直到 6 年前
        1
  •  1
  •   Marc LaFleur    6 年前

    在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}'
    

    这里需要注意的是 /成员 支持按筛选 userPrincipalName 身份证件 .

        2
  •  1
  •   Seiya Su    6 年前

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

    使用以下代码获取具有DirectoryRole的列表,然后按DisplayName筛选,然后检查 角色模板id (对于来自的directoryRole) 我的会员 管理员 ,基本上,我们是管理员角色。如果使用 目录角色 角色模板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();
    

    的结果 : enter image description here 目录角色 enter image description here

    如果解决方案仍然不能满足您的要求,我建议您在 uservoice github issues

    补充回答: (遗憾的是,在Microsoft Graph for directory resources中,过滤通常非常有限。因此,您可以使用客户端内存过滤器或立即提交功能请求)

    )

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

    在基于图形客户端的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);