Пример #1
0
        private static List <IAccount> UpdateWithAdalAccounts(
            string envFromRequest,
            IEnumerable <string> envAliases,
            AdalUsersForMsal adalUsers,
            IDictionary <string, Account> clientInfoToAccountMap)
        {
            var accounts = new List <IAccount>();

            foreach (KeyValuePair <string, AdalUserInfo> pair in adalUsers.GetUsersWithClientInfo(envAliases))
            {
                var    clientInfo        = ClientInfo.CreateFromJson(pair.Key);
                string accountIdentifier = clientInfo.ToAccountIdentifier();

                if (!clientInfoToAccountMap.ContainsKey(accountIdentifier))
                {
                    clientInfoToAccountMap[accountIdentifier] = new Account(
                        accountIdentifier, pair.Value.DisplayableId, envFromRequest);
                }
            }

            accounts.AddRange(clientInfoToAccountMap.Values);
            var uniqueUserNames = clientInfoToAccountMap.Values.Select(o => o.Username).Distinct().ToList();

            foreach (AdalUserInfo user in adalUsers.GetUsersWithoutClientInfo(envAliases))
            {
                if (!string.IsNullOrEmpty(user.DisplayableId) && !uniqueUserNames.Contains(user.DisplayableId))
                {
                    accounts.Add(new Account(null, user.DisplayableId, envFromRequest));
                    uniqueUserNames.Add(user.DisplayableId);
                }
            }

            return(accounts);
        }
Пример #2
0
        private static void AssertByUsername(
            AdalUsersForMsal adalUsers,
            IEnumerable <string> enviroments,
            IEnumerable <string> expectedUsersWithClientInfo,
            IEnumerable <string> expectedUsersWithoutClientInfo)
        {
            // Assert
            var usersWithClientInfo = adalUsers.GetUsersWithClientInfo(enviroments).Select(x => x.Value);
            IEnumerable <AdalUserInfo> usersWithoutClientInfo = adalUsers.GetUsersWithoutClientInfo(enviroments);

            AssertUsersByDisplayName(
                expectedUsersWithClientInfo,
                usersWithClientInfo,
                "Expecting only user1 and user2 because the other users either have no ClientInfo or have a different env or clientid");
            AssertUsersByDisplayName(expectedUsersWithoutClientInfo, usersWithoutClientInfo);
        }
Пример #3
0
        private static void UpdateMapWithAdalAccountsWithClientInfo(
            string envFromRequest,
            IEnumerable <string> envAliases,
            AdalUsersForMsal adalUsers,
            IDictionary <string, Account> clientInfoToAccountMap)
        {
            foreach (KeyValuePair <string, AdalUserInfo> pair in adalUsers.GetUsersWithClientInfo(envAliases))
            {
                var    clientInfo        = ClientInfo.CreateFromJson(pair.Key);
                string accountIdentifier = clientInfo.ToAccountIdentifier();

                if (!clientInfoToAccountMap.ContainsKey(accountIdentifier))
                {
                    clientInfoToAccountMap[accountIdentifier] = new Account(
                        accountIdentifier, pair.Value.DisplayableId, envFromRequest);
                }
            }
        }
        [TestCategory(TestCategories.Regression)] // https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/1815

        public async Task UnifiedCache_ProcessAdalDictionaryForDuplicateKey_Async()
        {
            using (var harness = CreateTestHarness())
            {
                var app = PublicClientApplicationBuilder
                          .Create(TestConstants.ClientId)
                          .WithAuthority(new Uri(ClientApplicationBase.DefaultAuthority), true)
                          .WithUserTokenLegacyCachePersistenceForTest(new TestLegacyCachePersistance())
                          .WithHttpManager(harness.HttpManager)
                          .BuildConcrete();

                CreateAdalCache(harness.ServiceBundle.ApplicationLogger, app.UserTokenCacheInternal.LegacyPersistence, TestConstants.s_scope.ToString());

                var adalUsers =
                    CacheFallbackOperations.GetAllAdalUsersForMsal(
                        harness.ServiceBundle.ApplicationLogger,
                        app.UserTokenCacheInternal.LegacyPersistence,
                        TestConstants.ClientId);

                CreateAdalCache(harness.ServiceBundle.ApplicationLogger, app.UserTokenCacheInternal.LegacyPersistence, "user.read");

                AdalUsersForMsal adalUsers2 =
                    CacheFallbackOperations.GetAllAdalUsersForMsal(
                        harness.ServiceBundle.ApplicationLogger,
                        app.UserTokenCacheInternal.LegacyPersistence,
                        TestConstants.ClientId);

                Assert.AreEqual(
                    adalUsers.GetUsersWithClientInfo(null).Single().Key,
                    adalUsers2.GetUsersWithClientInfo(null).Single().Key);

                var accounts = await app.GetAccountsAsync().ConfigureAwait(false);

                Assert.AreEqual(1, accounts.Count(), "The 2 RTs belong to the same user");

                // The ADAL cache contains access tokens, but these are NOT usable by MSAL / v2 endpoint.
                // MSAL will however use the RT from ADAL to fetch new access tokens...
                harness.HttpManager.AddAllMocks(TokenResponseType.Valid);
                var res = await app.AcquireTokenSilent(TestConstants.s_scope, accounts.First()).ExecuteAsync().ConfigureAwait(false);

                Assert.IsNotNull(res);
            }
        }