Microsoft Graph - Issues when trying to upgrade from 3.19.0 to v5.56.0

Terry Fedirko 0 Reputation points
2024-06-18T15:31:44.6333333+00:00

I got a problem when trying to upgrade GraphServiceClient from 3.19.0 to 5.56.0 (as shown below), how do I update this code to Microsoft.Graph 5.56? Please note, I find all examples on the web and they don't show me a solution:

   protected GraphServiceClient graphServiceClient

   {

       get

       {

           GraphServiceClient gsc = new GraphServiceClient(

               new DelegateAuthenticationProvider(

                   (requestMessage) =>

                   {

                       requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", GetUserToken().GetAwaiter().GetResult());

                       return Task.FromResult(0);

                   }));

           return gsc;

       }

   }

   protected async Task<string> GetUserToken()

   {

       string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;

       string tenantID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;

       string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;

       // get a token for the Graph without triggering any user interaction (from the cache, via multi-resource refresh token, etc)

       Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential clientCredentials =

           new Microsoft.IdentityModel.Clients.ActiveDirectory

               .ClientCredential(Config.ADSettings.EquiTrakAppId, Config.ADSettings.EquiTrakClientSecret);

       // initialize AuthenticationContext with the token cache of the currently signed in user, as kept in the app's database

       AuthenticationContext authenticationContext =

           new AuthenticationContext(Config.AzureADInstance + tenantID, new ADALTokenCache(signedInUserID));

       Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationResult authenticationResult =

           await authenticationContext

               .AcquireTokenSilentAsync(Config.GraphResourceId, clientCredentials, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));

       return authenticationResult.AccessToken;

   }
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,229 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,561 questions
{count} votes