/// <summary>
        /// Use the Active Directory Authentication Library to get and verify the token.
        /// </summary>
        public static async Task<AuthenticationResult> AcquireTokenSilentAsync(string clientID, string clientSecret, string objectIdentifier, string tenantID, TokenCache tokenCache)
        {
            var userId = new UserIdentifier(objectIdentifier, UserIdentifierType.UniqueId);
            var item = tokenCache.ReadItems().First(i => i.TenantId == tenantID);
            var context = new AuthenticationContext(item.Authority, tokenCache);

            var credential = new ClientCredential(clientID, clientSecret);
            AuthenticationResult result;

            try
            {
                result = await context.AcquireTokenSilentAsync(Keys.Resource, credential, userId);
            }
            catch (AdalSilentTokenAcquisitionException)
            {
                throw;
            }

            return result;
        }
        private static void VerifyCacheItems(TokenCache cache, int expectedCount, TokenCacheKey firstKey, TokenCacheKey secondKey)
        {
            var items = cache.ReadItems().ToList();
            Verify.AreEqual(expectedCount, items.Count);

            if (firstKey != null)
            {
                Verify.IsTrue(AreEqual(items[0], firstKey) || AreEqual(items[0], secondKey));
            }

            if (secondKey != null)
            {
                Verify.IsTrue(AreEqual(items[1], firstKey) || AreEqual(items[1], secondKey));
            }
        }
        internal static void TokenCacheSerializationTest()
        {
            var context = new AuthenticationContext("https://login.windows.net/common", false);
            var tokenCache = context.TokenCache;
            const int MaxItemCount = 100;
            const int MaxFieldSize = 1024;

            for (int i = 0; i < 100; i++)
            {
                tokenCache.Clear();
                for (int count = 0; count < Rand.Next(1, MaxItemCount); count++)
                {
                    TokenCacheKey key = GenerateRandomTokenCacheKey(MaxFieldSize);

                    AuthenticationResultEx result = GenerateRandomCacheValue(MaxFieldSize);
                    AddToDictionary(tokenCache, key, result);
                }

                byte[] serializedCache = tokenCache.Serialize();
                TokenCache tokenCache2 = new TokenCache(serializedCache);
                Verify.AreEqual(tokenCache.Count, tokenCache2.Count);
                foreach (TokenCacheItem item in tokenCache.ReadItems())
                {
                    var item2 = tokenCache2.ReadItems().FirstOrDefault(it => it.AccessToken == item.AccessToken);
                    Verify.IsNotNull(item2);
                    double diff = Math.Abs((item.ExpiresOn - item2.ExpiresOn).TotalSeconds);
                    Verify.IsLessThanOrEqual(diff, 1.0);
                }
            }
        }