在 Graph API 中搜索用户

Dav*_*ave 1 microsoft-graph-api

在我的 C# 应用程序中,我尝试通过 Graph API 搜索用户。我唯一的参数是存储在 onPremisesSamAccountName 字段中的用户名。

通过 Graph Explorer 我可以成功运行查询

https://graph.microsoft.com/v1.0/users?$count=true&$search="onPremisesSamAccountName:myusername"&$select=id,displayName

Graph Explorer 为我提供了要使用的 C# 代码

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var users = await graphClient.Users
    .Request()
    .Search("onPremisesSamAccountName:myusername")
    .Select("id,displayName")
    .GetAsync();
Run Code Online (Sandbox Code Playgroud)

现在,当我尝试使用该代码时,我收到一条错误消息,指出“搜索不是一种方法”,我是否需要添加额外的包才能使用“搜索”?

use*_*152 5

我也没有找到任何带有Search方法的 nuget 包。

您可以使用查询选项指定搜索值。$search 查询参数需要请求标头 ConsistencyLevel: eventual

var queryOptions = new List<Option>()
        {
            new QueryOption("$search", "\"onPremisesSamAccountName:myusername\""),
            new HeaderOption("ConsistencyLevel", "eventual")
        };
        var users = await graphClient.Users
            .Request(queryOptions)
            .Select("id,displayName")
            .GetAsync();
Run Code Online (Sandbox Code Playgroud)