无法以静默方式获取令牌,因为在缓存中找不到令牌。调用方法 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 调用,我使用下面的代码从缓存中获取访问令牌:

 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

我可以重现你的错误。根据 this link , it seems that code sample doesn't work when using AcquireTokenSilentAsync(use NaiveSessionCache) , Please try to modify NaiveSessionCache.cs to this

请告诉我是否有帮助。