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/authorize
when call AcquireTokenSilent(). This way the same code above works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With