c# - AcquireTokenSilent 始终无法静默获取 token

标签 c# azure oauth azure-active-directory adal

使用 ADAL,我有两个使用 SQL 中保存的 token 缓存的 AuthenticationContext

使用AcquireTokenByAuthorizationCode它将 token 写入数据库,但是当使用AcquireTokenSilent时我总是得到

Failed to acquire token silently. Call method AcquireToken

以下是复制问题的详细信息:

我创建一个上下文

AuthenticationContext authContext = new AuthenticationContext(_authority, new AzureAdalCache(companyId, _entries, _unitOfWork));

然后我通过授权获取Token

authContext.AcquireTokenByAuthorizationCode(authorizationCode, new Uri(redirectUri), _clientCredential);

此时,它在数据库中保存了一个条目

然后,如果我调用它,我会得到一个异常。

authContext.AcquireTokenSilent(_authority, _clientCredential, new UserIdentifier(companyId.ToString(), UserIdentifierType.UniqueId)).AccessToken;

我也尝试过,结果相同:

authContext.AcquireTokenSilent(_authority, _clientId).AccessToken;
authContext.AcquireTokenSilent(_authority, _clientCredential, UserIdentifier.AnyUser).AccessToken;

我在 this Gist 中发布了我的 AzureAdalCache 实现.

缓存的每个条目是 like this .

我错过了什么?

更新

根据@vibronet评论的回答,我有这个

AuthenticationContext authContext = new AuthenticationContext(_authority, new AzureAdalCache(companyId, _entries, _unitOfWork));
authContext.AcquireTokenByAuthorizationCode(authorizationCode, new Uri(redirectUri), _clientCredential, _eWSResource);
string result = authContext.AcquireTokenSilent(_eWSResource, _clientId, UserIdentifier.AnyUser).AccessToken;

最佳答案

问题是我基本上使用的是 Common Authority https://login.windows.net/common/oauth2/authorize在我的应用程序中。它适用于 AcquireTokenByAuthorizationCode(),但不适用于 AcquireTokenSilent()。

所以我需要它在调用 AcquireTokenByAuthorizationCode() 时保存 TenantId 并且权限使用像 https://login.windows.net/<tenant ID>/oauth2/authorize 这样的权限当调用 AcquireTokenSilent() 时。这样上面的代码就可以工作了。

关于c# - AcquireTokenSilent 始终无法静默获取 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29740339/

相关文章:

ios - 使用第 3 方 OAuth token 在自定义用户数据库中创建用户对象?

c# - Xamarin.Forms 自定义日期时间 BindableProperty BindingPropertyChangedDelegate

visual-studio-2010 - 如何在不重新启动 Windows Azure 模拟器的情况下编辑和查看更改?

azure - VSTS 数据库部署到 Azure - 循环任务组(或类似的东西)

azure - 如何从 azure 数据表中以解密格式获取加密的机器人数据

asp.net - Umbraco 前端的 Oauth 授权

c# - List<T> 动态拼接

c# - 如何创建非矩形窗体?

c# - 在 EXCEL VBA 中使用 C# dll

ios - 刷新 super token Swift