示例#1
0
        public virtual async ValueTask <AuthenticationResult> AcquireTokenSilentAsync(
            string[] scopes,
            AuthenticationAccount account,
            string tenantId,
            string redirectUri,
            bool async,
            CancellationToken cancellationToken)
        {
            var result = await AcquireTokenSilentCoreAsync(scopes, account, tenantId, redirectUri, async, cancellationToken).ConfigureAwait(false);

            LogAccountDetails(result);
            return(result);
        }
示例#2
0
        private async ValueTask <IAccount> GetAccountAsync(string tenantId, 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);

            if (accounts.Count == 0)
            {
                throw new CredentialUnavailableException(NoAccountsInCacheMessage);
            }

            // 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 _skipTenantValidation is false and _tenantId is specified it must match the account
                                                              (_skipTenantValidation || string.IsNullOrEmpty(tenantId) || string.Compare(a.HomeAccountId?.TenantId, tenantId, StringComparison.OrdinalIgnoreCase) == 0)
                                                              )
                                               .ToList();

            if (_skipTenantValidation && filteredAccounts.Count > 1)
            {
                filteredAccounts = filteredAccounts
                                   .Where(a => string.IsNullOrEmpty(tenantId) || string.Compare(a.HomeAccountId?.TenantId, tenantId, StringComparison.OrdinalIgnoreCase) == 0)
                                   .ToList();
            }

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

            account = filteredAccounts[0];
            asyncLock.SetValue(account);
            return(account);
        }
        public virtual async ValueTask <AuthenticationResult> AcquireTokenSilentAsync(
            string[] scopes,
            AuthenticationAccount account,
            string tenantId,
            string redirectUri,
            bool async,
            CancellationToken cancellationToken)
        {
            IConfidentialClientApplication client = await GetClientAsync(async, cancellationToken).ConfigureAwait(false);

            var builder = client.AcquireTokenSilent(scopes, account);

            if (!string.IsNullOrEmpty(tenantId))
            {
                builder.WithAuthority(Pipeline.AuthorityHost.AbsoluteUri, tenantId);
            }
            return(await builder
                   .ExecuteAsync(async, cancellationToken)
                   .ConfigureAwait(false));
        }