Search code examples
c#microsoft-graph-apimicrosoft-graph-sdks

Filtering in a query for onPremisesImmutableId no longer works after upgrading to Microsoft Graph .NET SDK v5


Before the upgrade, we used the DelegateAuthenticationProvider. I replaced it according to these upgrade instructions.

private static GraphServiceClient CreateClient()
{
    var accessTokenProvider = new BaseBearerTokenAuthenticationProvider(new TokenProvider());

    return new GraphServiceClient(accessTokenProvider);
}

private class TokenProvider : IAccessTokenProvider
{
    public async Task<string> GetAuthorizationTokenAsync(Uri uri, Dictionary<string, object> additionalAuthenticationContext = default,
        CancellationToken cancellationToken = default)
    {
        AuthenticationResult token = await Global.Msal.AcquireTokenAsync(Global.MsalScopes.MicrosoftGraph);

        return token.AccessToken;
    }

    public AllowedHostsValidator AllowedHostsValidator { get; }
}

I migrated the query as follows, also according to the instructions:

var client = Global.MicrosoftGraph.Client;
string base64Guid = Convert.ToBase64String(guid.ToByteArray());

var result = await client.Users.GetAsync(requestConfiguration =>
{
    requestConfiguration.QueryParameters.Filter = $"onPremisesImmutableId eq '{System.Web.HttpUtility.UrlEncode(base64Guid)}'";
    requestConfiguration.QueryParameters.Select = new string[] { "id" };
});

If I use the filter parameter onPremisesImmutableId replace with surname the query works.

If I use the v4 again with the DelegateAuthenticationProvider, the code works as well.


Solution

  • The new SDK v5 does the UrlEncode automatically. As soon as you remove the manual encoding from version v4, the code works.