AcquireTokenSilent始终无法以静默方式获取令牌 [英] AcquireTokenSilent always Failed to acquire token silently

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

问题描述

使用ADAL,我有两个使用令牌缓存的 AuthenticationContext 保留在SQL中。

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

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

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

无法静默获取令牌。调用方法AcquireToken

Failed to acquire token silently. Call method AcquireToken

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

Here are the details for replication the issue:

我创建了一个上下文

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;

我将 AzureAdalCache 实现发布到此要点

缓存的每个条目喜欢

什么是我想念吗?

更新

基于@vibronet的评论我

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;


推荐答案

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

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().

因此,当调用AcquireTokenByAuthorizationCode()时,我需要它来保存TenantId,并且授权使用诸如<$的授权c $ c> https://login.windows.net/<tenant ID> / oauth2 / authorize ,当调用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天全站免登陆