0
votes

I'm trying to get the MS example application to work for the Graph Beta Webhooks API and it's currently crashing because I've had to to modify some of the example code to remove some obsolete code and I'm not sure what to replace it with. This is the function:

    public static GraphServiceClient GetAuthenticatedClient(string userId, string redirect)
    {
        var graphClient = new GraphServiceClient(
            new DelegateAuthenticationProvider(
                async (request) =>
                {
                    var tokenCache = new SampleTokenCache(userId);

                    // Obsolete code
                    //var cca = new ConfidentialClientApplication(Startup.ClientId, redirect, new ClientCredential(Startup.ClientSecret), tokenCache.GetMsalCacheInstance(), null);
                    // New code
                    var cca2 = ConfidentialClientApplicationBuilder.Create(Startup.ClientId).WithClientSecret(Startup.ClientSecret).WithRedirectUri(redirect).Build();
                    // Question - how do I pass the tokenCache in here as the userTokenCache ?

                    // ERROR - With the new code this returns zero accounts presuambly because I haven't passed in a userTokenCache
                    var accounts = await cca2.GetAccountsAsync();

                    // Obsolete code
                    //var authResult = await cca.AcquireTokenSilentAsync(Startup.Scopes, accounts.First());
                    // New code
                    var authResult2 = await cca2.AcquireTokenSilent(Startup.Scopes, accounts.First()).ExecuteAsync();

                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authResult2.AccessToken);
                }));

        return graphClient;
    }

If I use the ConfidentialClientApplicationBuilder then the GetAccountsAsync() returns an empty collection and I think it's because I haven't passed the tokenCache into the builder. Does anyone know how to fix this code or has anyone got the example App working ? This is the link to the example App:

https://docs.microsoft.com/en-us/samples/microsoftgraph/aspnet-webhooks-rest-sample/microsoft-graph-aspnet-webhooks/

Thanks Ed James

1

1 Answers

0
votes

You need to login as user so that an account is added to the token cache which is available calling the tokenCache.GetMsalCacheInstance() method.