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 的结果:
如果解决方法仍然不能满足您的要求,我建议您在uservoice 和github 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);