Phoenix
Phoenix

Reputation: 127

Failed to acquire token silently as no token was found in the cache. Call method AcquireToken

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

Upvotes: 2

Views: 5660

Answers (1)

Nan Yu
Nan Yu

Reputation: 27588

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 .

Upvotes: 2

Related Questions