public void VerifyCacheKeyComparison_NotEqual()
        {
            var cacheKeyLower = new CredentialCacheKey
            {
                AccountType = AccountType.MicrosoftAccount,
                UserId = "abc",
            };

            var cacheKeyUpper = new CredentialCacheKey
            {
                AccountType = AccountType.MicrosoftAccount,
                ClientId = "CLIENTID",
                UserId = "ABC",
            };

            Assert.AreNotEqual(cacheKeyLower, cacheKeyUpper, "Cache key comparison failed.");
        }
Exemplo n.º 2
0
        internal virtual AccountSession GetResultFromCache(AccountType accountType, string clientId, string userId)
        {
            var cacheNotificationArgs = new CredentialCacheNotificationArgs {
                CredentialCache = this
            };

            this.OnBeforeAccess(cacheNotificationArgs);

            var credentialCacheKey = new CredentialCacheKey
            {
                AccountType = accountType,
                ClientId    = clientId,
                UserId      = userId,
            };

            AccountSession cacheResult = null;

            this.cacheDictionary.TryGetValue(credentialCacheKey, out cacheResult);

            this.OnAfterAccess(cacheNotificationArgs);

            return(cacheResult);
        }
        internal virtual AccountSession GetResultFromCache(AccountType accountType, string clientId, string userId)
        {
            var cacheNotificationArgs = new CredentialCacheNotificationArgs { CredentialCache = this };
            this.OnBeforeAccess(cacheNotificationArgs);

            var credentialCacheKey = new CredentialCacheKey
            {
                AccountType = accountType,
                ClientId = clientId,
                UserId = userId,
            };

            AccountSession cacheResult = null;
            this.cacheDictionary.TryGetValue(credentialCacheKey, out cacheResult);

            this.OnAfterAccess(cacheNotificationArgs);

            return cacheResult;
        }
        public void VerifyBlobSerialization()
        {
            var accountSessions = new List<AccountSession>
            {
                new AccountSession
                {
                    AccessToken = "token",
                    ClientId = "1",
                    ExpiresOnUtc = DateTimeOffset.Now,
                    RefreshToken = "refresh",
                    Scopes = new string[] { "scope1", "scope2" },
                    UserId = "1",
                },
                new AccountSession
                {
                    AccessToken = "token2",
                    ClientId = "2",
                    ExpiresOnUtc = DateTimeOffset.Now,
                    RefreshToken = "refresh2",
                    Scopes = new string[] { "scope" },
                    UserId = "2",
                    AccountType = AccountType.MicrosoftAccount,
                }
            };

            foreach (var accountSession in accountSessions)
            {
                this.credentialCache.AddToCache(accountSession);
            }

            var cacheBlob = this.credentialCache.GetCacheBlob();
            var newCredentialCache = new CredentialCache(cacheBlob);

            Assert.AreEqual(2, newCredentialCache.cacheDictionary.Count, "Unexpected number of cache entries.");

            foreach (var accountSession in accountSessions)
            {
                var accountSessionKey = new CredentialCacheKey
                {
                    AccountType = accountSession.AccountType,
                    ClientId = accountSession.ClientId,
                    UserId = accountSession.UserId,
                };

                var sessionFromCacheDictionary = newCredentialCache.cacheDictionary[accountSessionKey];

                Assert.IsNotNull(sessionFromCacheDictionary, "Unexpected account session returned.");
                Assert.AreEqual(accountSession.AccessToken, sessionFromCacheDictionary.AccessToken, "Unexpected access token returned.");
                Assert.AreEqual(accountSession.AccountType, sessionFromCacheDictionary.AccountType, "Unexpected account type returned.");
                Assert.AreEqual(accountSession.ClientId, sessionFromCacheDictionary.ClientId, "Unexpected client ID returned.");
                Assert.AreEqual(accountSession.ExpiresOnUtc, sessionFromCacheDictionary.ExpiresOnUtc, "Unexpected expiration returned.");
                Assert.AreEqual(accountSession.RefreshToken, sessionFromCacheDictionary.RefreshToken, "Unexpected refresh token returned.");
                Assert.AreEqual(accountSession.UserId, sessionFromCacheDictionary.UserId, "Unexpected access token returned.");
                Assert.AreEqual(accountSession.Scopes.Length, sessionFromCacheDictionary.Scopes.Length, "Unexpected scopes returned.");

                for (int i = 0; i < accountSession.Scopes.Length; i++)
                {
                    Assert.AreEqual(accountSession.Scopes[i], sessionFromCacheDictionary.Scopes[i], "Unexpected scope returned.");
                }
            }
        }