我使用此代码示例:https://github.com/Azure-Samples/active-directory-dotnet-graphapi-web ,我知道此代码示例正在使用客户端库,但如果我想直接使用 api 调用执行查询(使用 httpclient),我使用下面的代码从缓存中获取访问 token :
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));
但我发现总是抛出错误:无法静默获取 token ,因为在缓存中找不到 token 。调用AcquireToken方法
最佳答案
我可以重现您的错误。根据this link ,使用 AcquireTokenSilentAsync(use NaiveSessionCache) 时代码示例似乎不起作用,请尝试将 NaiveSessionCache.cs 修改为 this 。
请告诉我是否有帮助。
关于azure-active-directory - 由于在缓存中未找到 token ,无法以静默方式获取 token 。调用AcquireToken方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42830016/