/// <summary>
        /// Gets the current set of accounts in the cache by creating a new public client, and
        /// deserializing the cache into a temporary object.
        /// </summary>
        private static async Task <HashSet <string> > GetAccountIdentifiersAsync(StorageCreationProperties storageCreationProperties)
        {
            var accountIdentifiers = new HashSet <string>();

            if (File.Exists(storageCreationProperties.CacheFilePath))
            {
                var pca = PublicClientApplicationBuilder.Create(storageCreationProperties.ClientId).Build();

                pca.UserTokenCache.SetBeforeAccess((args) =>
                {
                    var tempCache = new MsalCacheStorage(storageCreationProperties, s_staticLogger.Value);
                    // We're using ReadData here so that decryption is gets handled within the store.
                    args.TokenCache.DeserializeMsalV3(tempCache.ReadData());
                });

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

                foreach (var account in accounts)
                {
                    accountIdentifiers.Add(account.HomeAccountId.Identifier);
                }
            }

            return(accountIdentifiers);
        }
        /// <summary>
        /// An internal constructor allowing unit tests to data explicitly rather than initializing here.
        /// </summary>
        /// <param name="userTokenCache">The token cache to synchronize with the backing store</param>
        /// <param name="store">The backing store to use.</param>
        /// <param name="logger">Passing null uses the default logger</param>
        internal MsalCacheHelper(ITokenCache userTokenCache, MsalCacheStorage store, TraceSource logger = null)
        {
            _logger    = logger == null ? s_staticLogger.Value : new TraceSourceLogger(logger);
            CacheStore = store;
            _storageCreationProperties = store.StorageCreationProperties;

            RegisterCache(userTokenCache);
        }
Пример #3
0
        /// <summary>
        /// Creates a new instance of this class.
        /// </summary>
        /// <param name="storageCreationProperties">Properties to use when creating storage on disk.</param>
        /// <param name="logger">Passing null uses a default logger</param>
        /// <param name="knownAccountIds">The set of known accounts</param>
        /// <param name="cacheWatcher">Watcher for the cache file, to enable sending updated events</param>
        private MsalCacheHelper(StorageCreationProperties storageCreationProperties, TraceSource logger, HashSet <string> knownAccountIds, FileSystemWatcher cacheWatcher)
        {
            _logger = logger ?? s_staticLogger.Value;
            _storageCreationProperties = storageCreationProperties;
            _store           = new MsalCacheStorage(_storageCreationProperties, _logger);
            _knownAccountIds = knownAccountIds;

            _cacheWatcher          = cacheWatcher;
            _cacheWatcher.Changed += OnCacheFileChangedAsync;
            _cacheWatcher.Deleted += OnCacheFileChangedAsync;
        }
        /// <summary>
        /// Gets the current set of accounts in the cache by creating a new public client, and
        /// deserializing the cache into a temporary object.
        /// </summary>
        private static async Task <HashSet <string> > GetAccountIdentifiersAsync(
            StorageCreationProperties storageCreationProperties,
            TraceSourceLogger logger)
        {
            var accountIdentifiers = new HashSet <string>();

            if (File.Exists(storageCreationProperties.CacheFilePath))
            {
                var pca = PublicClientApplicationBuilder.Create(storageCreationProperties.ClientId).Build();

                pca.UserTokenCache.SetBeforeAccess((args) =>
                {
                    MsalCacheStorage tempCache = null;
                    try
                    {
                        tempCache = MsalCacheStorage.Create(storageCreationProperties, s_staticLogger.Value.Source);
                        // We're using ReadData here so that decryption is handled within the store.
                        var data = tempCache.ReadData();
                        args.TokenCache.DeserializeMsalV3(data);
                    }
                    catch (Exception e)
                    {
                        logger.LogError("An error occured while reading the token cache: " + e);
                        logger.LogError("Deleting the token cache as it might be corrupt.");
                        tempCache.Clear();
                    }
                });

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

                foreach (var account in accounts)
                {
                    accountIdentifiers.Add(account.HomeAccountId.Identifier);
                }
            }

            return(accountIdentifiers);
        }
Пример #5
0
 /// <summary>
 /// Creates a new instance of this class.
 /// </summary>
 /// <param name="storageCreationProperties">Properties to use when creating storage on disk.</param>
 /// <param name="logger">Passing null uses the default logger</param>
 public MsalCacheHelper(StorageCreationProperties storageCreationProperties, TraceSource logger = null)
 {
     _logger = logger ?? s_staticLogger.Value;
     _storageCreationProperties = storageCreationProperties;
     _store = new MsalCacheStorage(_storageCreationProperties, logger);
 }