2
votes

I use this code sample :https://github.com/Azure-Samples/active-directory-dotnet-graphapi-web , i know this code sample is using client library , but if i want to perfrom a query (use httpclient) directly use api calls ,i used below code to get the access token from cache :

 string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; 
 AuthenticationContext authContext = new AuthenticationContext(Startup.Authority, new NaiveSessionCache(userObjectID)); 
 ClientCredential credential = new ClientCredential(clientId, appKey); 
 var result = await authContext.AcquireTokenSilentAsync(resource, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));

But i found always threw error :Failed to acquire token silently as no token was found in the cache. Call method AcquireToken

1
Have you tried to logout and login again to test this scenario?Nan Yu
just curious, why its throwing error like that and not getting a new token when the user is already authenticated?aj go

1 Answers

2
votes

I could reproduce your error. According to this link , it seems that code sample doesn't work when using AcquireTokenSilentAsync(use NaiveSessionCache) , Please try to modify NaiveSessionCache.cs to this .

Please let me know whether it helps .