无法静默获取令牌,因为在缓存中未找到令牌。调用方法AcquireToken [英] Failed to acquire token silently as no token was found in the cache. Call method AcquireToken
问题描述
我使用以下代码示例: https://github.com / Azure-Samples / active-directory-dotnet-graphapi-web ,我知道此代码示例正在使用客户端库,但是如果我要执行查询(使用httpclient)直接使用api调用,则在下面的代码中使用从缓存中获取访问令牌:
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));
但是我发现总是抛出错误:由于在缓存中未找到令牌,因此无法静默获取令牌。调用方法AcquireToken
But i found always threw error :Failed to acquire token silently as no token was found in the cache. Call method AcquireToken
推荐答案
我可以重现您的错误。根据此链接,看来使用AcquireTokenSilentAsync(使用NaiveSessionCache)时代码示例不起作用,请尝试将NaiveSessionCache.cs修改为此。
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 .
这篇关于无法静默获取令牌,因为在缓存中未找到令牌。调用方法AcquireToken的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!