AcquireTokenSilent 总是 静默获取令牌失败 [英] AcquireTokenSilent always Failed to acquire token silently

查看:20
本文介绍了AcquireTokenSilent 总是 静默获取令牌失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 ADAL,我有两个 AuthenticationContext 使用 SQL 中持久化的令牌缓存.

使用 AcquireTokenByAuthorizationCode 它在数据库中写入令牌,但是当使用 AcquireTokenSilent 我总是得到

<块引用>

静默获取令牌失败.调用方法 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;

我将我的 AzureAdalCache 实现发布在 this Gist 中.p>

Cache的每一项都是like this.

我错过了什么?

更新

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

AuthenticationContext authContext = new AuthenticationContext(_authority, new AzureAdalCache(companyId, _entries, _unitOfWork));authContext.AcquireTokenByAuthorizationCode(authorizationCode, new Uri(redirectUri), _clientCredential, _eWSResource);字符串结果 = 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().这样,上面的代码就可以工作了.

Using ADAL I have two AuthenticationContext using a Token Cache persisted in SQL.

Using AcquireTokenByAuthorizationCode it writes the Token in database, but when using AcquireTokenSilent I always get

Failed to acquire token silently. Call method AcquireToken

Here are the details for replication the issue:

I create a Context

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

Then I AcquireToken By Authorization

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

At this point, it saves an entry in the database

Then if I call this I get an exception.

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

I also tried with the same result:

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

I Post my AzureAdalCache implementation in this Gist.

Each entry of the Cache is like this.

What Am I missing?

Update

Based on answer of comments of @vibronet I have this

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;

解决方案

The issue was that basically I was using Common Authority https://login.windows.net/common/oauth2/authorize in my App. It works for AcquireTokenByAuthorizationCode() but not for AcquireTokenSilent().

So I needed it to save the TenantId when call AcquireTokenByAuthorizationCode() and an authority use an authority like https://login.windows.net/<tenant ID>/oauth2/authorizewhen call AcquireTokenSilent(). This way the same code above works.

这篇关于AcquireTokenSilent 总是 静默获取令牌失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆