Beispiel #1
0
        private bool assertSameTokenStreamRegardlessOfPosition(Token[] expected, Token[] actual)
        {
            if (expected.Length != actual.Length)
            {
                difference     = TokenDifference.StreamLength;
                differentIndex = 0;
                return(false);
            }

            for (int i = 0; i < expected.Length; i++)
            {
                var ac = actual[i];
                var ex = expected[i];

                if (ex.Type != ac.Type)
                {
                    difference     = TokenDifference.Type;
                    differentIndex = i;
                    return(false);
                }

                if (ex.Value != ac.Value)
                {
                    difference     = TokenDifference.Value;
                    differentIndex = i;
                    return(false);
                }
            }

            return(true);
        }
        private void PopulateAppCache(ConfidentialClientApplication cca, TokenDifference tokenDifference, int size)
        {
            Dictionary <string, InMemoryTokenCacheAccessor> accessors = new Dictionary <string, InMemoryTokenCacheAccessor>();
            string key = "";

            for (int i = 0; i < size; i++)
            {
                string tenantId = "tid";
                string scope    = "scope";

                switch (tokenDifference)
                {
                case TokenDifference.ByScope:
                    scope = $"scope_{i}";
                    break;

                case TokenDifference.ByTenant:
                    tenantId = $"tid_{i}";
                    break;

                default:
                    throw new NotImplementedException();
                }

                MsalAccessTokenCacheItem atItem = new MsalAccessTokenCacheItem(
                    TestConstants.ProductionPrefCacheEnvironment,
                    TestConstants.ClientId,
                    scope,
                    tenantId,
                    "",
                    new DateTimeOffset(DateTime.UtcNow + TimeSpan.FromSeconds(3600)),
                    new DateTimeOffset(DateTime.UtcNow + TimeSpan.FromSeconds(3600)),
                    null,
                    null);

                key = SuggestedWebCacheKeyFactory.GetClientCredentialKey(atItem.ClientId, atItem.TenantId);
                InMemoryTokenCacheAccessor accessor;
                if (!accessors.TryGetValue(key, out accessor))
                {
                    accessor       = new InMemoryTokenCacheAccessor(new NullLogger());
                    accessors[key] = accessor;
                }

                accessor.SaveAccessToken(atItem);

                if (tokenDifference == TokenDifference.ByTenant || (tokenDifference == TokenDifference.ByScope && i == size - 1))
                {
                    byte[] bytes = new TokenCacheJsonSerializer(accessor).Serialize(null);
                    cca.InMemoryPartitionedCacheSerializer.CachePartition[key] = bytes;
                }
            }

            // force a cache read, otherwise MSAL won't have the tokens in memory
            // force a cache read
            var args = new TokenCacheNotificationArgs(
                cca.AppTokenCacheInternal,
                cca.AppConfig.ClientId,
                null,
                hasStateChanged: false,
                true,
                hasTokens: true,
                cancellationToken: CancellationToken.None,
                suggestedCacheKey: key);

            cca.AppTokenCacheInternal.OnBeforeAccessAsync(args).GetAwaiter().GetResult();
        }