private async Task <IAccount> GetAccountAsync()
        {
            if (_record != null)
            {
                return(new AuthenticationAccount(_record));
            }

            List <IAccount> accounts = (await _client.GetAccountsAsync().ConfigureAwait(false)).ToList();

            // filter the accounts to those matching the specified user and tenant
            List <IAccount> filteredAccounts = accounts.Where(a =>
                                                              // if _username is specified it must match the account
                                                              (string.IsNullOrEmpty(_username) || string.Compare(a.Username, _username, StringComparison.OrdinalIgnoreCase) == 0)
                                                              &&
                                                              //if _tenantId is specified it must match the account
                                                              (string.IsNullOrEmpty(_tenantId) || string.Compare(a.HomeAccountId?.TenantId, _tenantId, StringComparison.OrdinalIgnoreCase) == 0)
                                                              ).ToList();

            if (filteredAccounts.Count != 1)
            {
                throw new CredentialUnavailableException(GetCredentialUnavailableMessage(accounts, filteredAccounts));
            }

            return(filteredAccounts.First());
        }
        private async Task <IAccount> GetAccountAsync()
        {
            List <IAccount> accounts = (await _client.GetAccountsAsync().ConfigureAwait(false)).ToList();

            List <IAccount> filteredAccounts = accounts.Where(a => (string.IsNullOrEmpty(_username) || a.Username == _username) && (string.IsNullOrEmpty(_tenantId) || a.HomeAccountId?.TenantId == _tenantId)).ToList();

            if (filteredAccounts.Count != 1)
            {
                throw new CredentialUnavailableException(GetCredentialUnavailableMessage(accounts, filteredAccounts));
            }

            return(filteredAccounts.First());
        }
        private async ValueTask <IAccount> GetAccountAsync(bool async, CancellationToken cancellationToken)
        {
            using var asyncLock = await _accountAsyncLock.GetLockOrValueAsync(async, cancellationToken).ConfigureAwait(false);

            if (asyncLock.HasValue)
            {
                return(asyncLock.Value);
            }

            IAccount account;

            if (_record != null)
            {
                account = new AuthenticationAccount(_record);
                asyncLock.SetValue(account);
                return(account);
            }

            List <IAccount> accounts = await _client.GetAccountsAsync(async, cancellationToken).ConfigureAwait(false);

            // filter the accounts to those matching the specified user and tenant
            List <IAccount> filteredAccounts = accounts.Where(a =>
                                                              // if _username is specified it must match the account
                                                              (string.IsNullOrEmpty(_username) || string.Compare(a.Username, _username, StringComparison.OrdinalIgnoreCase) == 0)
                                                              &&
                                                              //if _tenantId is specified it must match the account
                                                              (string.IsNullOrEmpty(_tenantId) || string.Compare(a.HomeAccountId?.TenantId, _tenantId, StringComparison.OrdinalIgnoreCase) == 0)
                                                              ).ToList();

            if (filteredAccounts.Count != 1)
            {
                throw new CredentialUnavailableException(GetCredentialUnavailableMessage(accounts, filteredAccounts));
            }

            account = filteredAccounts.First();
            asyncLock.SetValue(account);
            return(account);
        }