Microsoft graph api 在Graph API中搜索用户

Microsoft graph api 在Graph API中搜索用户,microsoft-graph-api,Microsoft Graph Api,在我的C#应用程序中,我试图通过Graph API搜索用户。我唯一的参数是存储在onPremisesSamAccountName字段中的用户名 通过图形浏览器,我可以成功运行查询 Graph Explorer给了我要使用的C代码 GraphServiceClient graphClient = new GraphServiceClient( authProvider ); var users = await graphClient.Users .Request() .Sear

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

通过图形浏览器,我可以成功运行查询

Graph Explorer给了我要使用的C代码

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

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

现在,当我尝试使用该代码时,我得到一个错误,说搜索不是一种方法,我需要添加一个额外的包来使用搜索吗?

我也没有找到任何使用
搜索
方法的nuget包

您可以使用查询选项指定搜索值$搜索查询参数需要请求标头一致性级别:最终

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();
var queryOptions=new List()
{
新的查询选项(“$search”,“\”onPremisesSamAccountName:myusername\”),
新磁头选项(“一致性级别”、“最终”)
};
var users=wait graphClient.users
.请求(查询选项)
.选择(“id,显示名称”)
.GetAsync();

谢谢。我刚试过,但我出错了。代码:BadRequest消息:语法错误:字符“:”在“onPremisesSamAccountName:username”中的位置24处无效。将其设置为新查询选项(“$search”,“\”onPremisesSamAccountName:username\”)它似乎可以工作,但我收到一条错误消息:带有$search查询参数的请求只能通过带有特殊请求标题的MSGraph工作:“ConsistenceLevel:Finally”@Dave我已经更新了答案并添加了ConsistenceLevel headerYup,这很有效。再次感谢