/// <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 = MsalCacheStorage.Create(storageCreationProperties, s_staticLogger.Value.Source); // We're using ReadData here so that decryption is gets handled within the store. var data = tempCache.ReadData(); args.TokenCache.DeserializeMsalV3(data); }); var accounts = await pca.GetAccountsAsync().ConfigureAwait(false); foreach (var account in accounts) { accountIdentifiers.Add(account.HomeAccountId.Identifier); } } return(accountIdentifiers); }
/// <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 == null ? s_staticLogger.Value : new TraceSourceLogger(logger); _storageCreationProperties = storageCreationProperties; CacheStore = MsalCacheStorage.Create(_storageCreationProperties, _logger.Source); _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); }