public static AdalResultWrapper FindMsalEntryForAdal(ITokenCacheAccessor tokenCacheAccessor, string authority,
                                                             string clientId, string upn, RequestContext requestContext)
        {
            try
            {
                var environment = new Uri(authority).Host;

                List <MsalAccountCacheItem> accounts = new List <MsalAccountCacheItem>();
                foreach (string accountStr in tokenCacheAccessor.GetAllAccountsAsString())
                {
                    var accountItem = JsonHelper.TryToDeserializeFromJson <MsalAccountCacheItem>(accountStr, requestContext);
                    if (accountItem != null && accountItem.Environment.Equals(environment, StringComparison.OrdinalIgnoreCase))
                    {
                        accounts.Add(accountItem);
                    }
                }
                if (accounts.Count > 0)
                {
                    foreach (var rtString in tokenCacheAccessor.GetAllRefreshTokensAsString())
                    {
                        var rtCacheItem =
                            JsonHelper.TryToDeserializeFromJson <MsalRefreshTokenCacheItem>(rtString, requestContext);

                        //TODO - authority check needs to be updated for alias check
                        if (rtCacheItem != null && environment.Equals(rtCacheItem.Environment, StringComparison.OrdinalIgnoreCase) &&
                            rtCacheItem.ClientId.Equals(clientId, StringComparison.OrdinalIgnoreCase))
                        {
                            // join refresh token cache item to corresponding account cache item to get upn
                            foreach (MsalAccountCacheItem accountCacheItem in accounts)
                            {
                                if (rtCacheItem.HomeAccountId.Equals(accountCacheItem.HomeAccountId, StringComparison.OrdinalIgnoreCase) &&
                                    accountCacheItem.PreferredUsername.Equals(upn, StringComparison.OrdinalIgnoreCase))
                                {
                                    return(new AdalResultWrapper
                                    {
                                        Result = new AdalResult(null, null, DateTimeOffset.MinValue),
                                        RefreshToken = rtCacheItem.Secret,
                                        RawClientInfo = rtCacheItem.RawClientInfo
                                    });
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MsalLogger.Default.WarningPiiWithPrefix(ex, "An error occurred while searching for refresh tokens in MSAL format in the cache for ADAL. " +
                                                        "For details please see https://aka.ms/net-cache-persistence-errors. ");
            }

            return(null);
        }
        /// <summary>
        /// Serializes the entire token cache
        /// </summary>
        /// <param name="tokenCacheAccessor">Token cache accessor to perform cache read operations</param>
        /// <returns>array of bytes containing the serialized cache</returns>
        internal static byte[] SerializeUnifiedCache(ITokenCacheAccessor tokenCacheAccessor)
        {
            // reads the underlying in-memory dictionary and dumps out the content as a JSON
            Dictionary <string, IEnumerable <string> > cacheDict = new Dictionary <string, IEnumerable <string> >
            {
                [AccessTokenKey]  = tokenCacheAccessor.GetAllAccessTokensAsString(),
                [RefreshTokenKey] = tokenCacheAccessor.GetAllRefreshTokensAsString(),
                [IdTokenKey]      = tokenCacheAccessor.GetAllIdTokensAsString(),
                [AccountKey]      = tokenCacheAccessor.GetAllAccountsAsString()
            };

            return(JsonHelper.SerializeToJson(cacheDict).ToByteArray());
        }
Example #3
0
 /// <inheritdoc />
 public ICollection <string> GetAllAccountsAsString()
 {
     return(_tokenCacheAccessor.GetAllAccountsAsString());
 }