protected async ValueTask <TClient> GetClientAsync(bool async, CancellationToken cancellationToken)
        {
            using (var asyncLock = await _clientAsyncLock.GetLockOrValueAsync(async, cancellationToken).ConfigureAwait(false)) {
                if (asyncLock.HasValue)
                {
                    return(asyncLock.Value);
                }

                var client = await CreateClientAsync(async, cancellationToken).ConfigureAwait(false);

                if (TokenCache != null)
                {
                    await TokenCache.RegisterCache(async, client.UserTokenCache, cancellationToken).ConfigureAwait(false);

                    if (client is IConfidentialClientApplication cca)
                    {
                        await TokenCache.RegisterCache(async, cca.AppTokenCache, cancellationToken).ConfigureAwait(false);
                    }
                }

                asyncLock.SetValue(client);
                return(client);
            }
        }
Example #2
0
        private async Task <MsalCacheHelperWrapper> GetCacheHelperAsync(bool async, CancellationToken cancellationToken)
        {
            using (var asyncLock = await cacheHelperLock.GetLockOrValueAsync(async, cancellationToken).ConfigureAwait(false))
            {
                if (asyncLock.HasValue)
                {
                    return(asyncLock.Value);
                }

                MsalCacheHelperWrapper cacheHelper;

                try
                {
                    cacheHelper = await GetProtectedCacheHelperAsync(async, _name).ConfigureAwait(false);

                    cacheHelper.VerifyPersistence();
                }
                catch (MsalCachePersistenceException)
                {
                    if (_allowUnencryptedStorage)
                    {
                        cacheHelper = await GetFallbackCacheHelperAsync(async, _name).ConfigureAwait(false);

                        cacheHelper.VerifyPersistence();
                    }
                    else
                    {
                        throw;
                    }
                }

                asyncLock.SetValue(cacheHelper);

                return(cacheHelper);
            }
        }