public IDictionary <string, JToken> Deserialize(byte[] bytes, bool clearExistingCacheData)
        {
            List <KeyValuePair <string, IEnumerable <string> > > cacheKvpList;

            try
            {
                cacheKvpList = JsonHelper.DeserializeFromJson <List <KeyValuePair <string, IEnumerable <string> > > >(bytes);
            }
            catch (Exception ex)
            {
                throw new MsalClientException(MsalError.JsonParseError, MsalErrorMessage.TokenCacheDictionarySerializerFailedParse, ex);
            }

            var cacheDict = cacheKvpList.ToDictionary(x => x.Key, x => x.Value);

            if (clearExistingCacheData)
            {
                _accessor.Clear();
            }

            if (cacheKvpList == null || cacheKvpList.Count == 0)
            {
                return(null);
            }

            if (cacheDict.ContainsKey(AccessTokenKey))
            {
                foreach (var atItem in cacheDict[AccessTokenKey])
                {
                    _accessor.SaveAccessToken(MsalAccessTokenCacheItem.FromJsonString(atItem));
                }
            }

            if (cacheDict.ContainsKey(RefreshTokenKey))
            {
                foreach (var rtItem in cacheDict[RefreshTokenKey])
                {
                    _accessor.SaveRefreshToken(MsalRefreshTokenCacheItem.FromJsonString(rtItem));
                }
            }

            if (cacheDict.ContainsKey(IdTokenKey))
            {
                foreach (var idItem in cacheDict[IdTokenKey])
                {
                    _accessor.SaveIdToken(MsalIdTokenCacheItem.FromJsonString(idItem));
                }
            }

            if (cacheDict.ContainsKey(AccountKey))
            {
                foreach (var account in cacheDict[AccountKey])
                {
                    _accessor.SaveAccount(MsalAccountCacheItem.FromJsonString(account));
                }
            }

            return(null);
        }
        public static void WriteMsalRefreshToken(ITokenCacheAccessor tokenCacheAccessor,
                                                 AdalResultWrapper resultWrapper, string authority, string clientId, string displayableId,
                                                 string givenName, string familyName, string objectId)
        {
            if (string.IsNullOrEmpty(resultWrapper.RawClientInfo))
            {
                MsalLogger.Default.Info("Client Info is missing. Skipping MSAL refresh token cache write");
                return;
            }

            if (string.IsNullOrEmpty(resultWrapper.RefreshToken))
            {
                MsalLogger.Default.Info("Refresh Token is missing. Skipping MSAL refresh token cache write");
                return;
            }

            if (string.IsNullOrEmpty(resultWrapper.Result.IdToken))
            {
                MsalLogger.Default.Info("Id Token is missing. Skipping MSAL refresh token cache write");
                return;
            }

            try
            {
                var rtItem = new MsalRefreshTokenCacheItem
                                 (new Uri(authority).Host, clientId, resultWrapper.RefreshToken, resultWrapper.RawClientInfo);
                tokenCacheAccessor.SaveRefreshToken(rtItem);

                MsalAccountCacheItem accountCacheItem = new MsalAccountCacheItem
                                                            (new Uri(authority).Host, objectId, resultWrapper.RawClientInfo, null, displayableId, resultWrapper.Result.TenantId,
                                                            givenName, familyName);
                tokenCacheAccessor.SaveAccount(accountCacheItem);
            }
            catch (Exception ex)
            {
                MsalLogger.Default.WarningPiiWithPrefix(
                    ex,
                    "An error occurred while writing ADAL refresh token to the cache in MSAL format. "
                    + "For details please see https://aka.ms/net-cache-persistence-errors. ");
            }
        }
 public static string GetKeyFromCachedItem(MsalAccountCacheItem accountCacheItem)
 {
     return(accountCacheItem.HomeAccountId);
 }