I am trying to retrieve user information from GraphAPI, in a Blazor page with .NET 8:
@inject GraphServiceClient GSC
@inject MicrosoftIdentityConsentAndConditionalAccessHandler ConsentHandler
// ...
protected async Task AzTest()
{
try
{
var users = await GSC.Users.Request()
.Filter("endswith(mail, 'mydomain.com')")
.GetAsync();
}
catch(Exception ex)
{
ConsentHandler.HandleException(ex);
}
}
However this gives a runtime error, Operator 'endsWith' is not supported because the 'ConsistencyLevel:eventual' header is missing.
and it refers me to the Graph API documentation.
Now, the documentation gives the example like:
var result = await graphClient.Users.GetAsync((requestConfiguration) =>
{
requestConfiguration.QueryParameters.Count = true;
requestConfiguration.Headers.Add("ConsistencyLevel", "eventual");
});
But this gives a compilation error: 'IGraphServiceUsersCollectionRequestBuilder' does not contain a definition for 'GetAsync'
...
If I change to to GSC.Users.Request().GetAsync(
with the same following parameters, the compilation error is: CS1660: Cannot convert lambda expression to type 'CancellationToken' because it is not a delegate type
.
What is the right code to enable these headers in C# ?
The NuGet packages I have installed are:
and none of those show as having updates available, so I presume they are the latest.
Microsoft.Identity.Web.MicrosoftGraph
is the legacy NuGet package using the Microsoft Graph SDK v4. If you use Microsoft.Identity.Web.GraphServiceClient
NuGet package then it's base on the Microsoft Graph SDK v5.
With Microsoft.Identity.Web.GraphServiceClient
and SDK v5, the request will look like this.
var result = await graphClient.Users.GetAsync((requestConfiguration) =>
{
requestConfiguration.QueryParameters.Filter = "endswith(mail, 'mydomain.com')";
requestConfiguration.QueryParameters.Count = true;
requestConfiguration.Headers.Add("ConsistencyLevel", "eventual");
});
If you use Microsoft.Identity.Web.MicrosoftGraph
and SDK v4, you need to specify query/header option
try
{
var options = new List<Option>
{
new QueryOption("$count", "true"),
new HeaderOption("ConsistencyLevel", "eventual")
};
var users = await GSC.Users.Request(options)
.Filter("endswith(mail, 'mydomain.com')")
.GetAsync();
}
catch(Exception ex)
{
ConsentHandler.HandleException(ex);
}