Esempio n. 1
0
        /// <summary>Initializes a new instance of the <see cref="MSALPerUserMemoryTokenCache"/> class.</summary>
        /// <param name="cache">The memory cache instance</param>
        public MSALPerUserMemoryTokenCacheProvider(IMemoryCache cache, MSALMemoryTokenCacheOptions option)
        {
            this.memoryCache = cache;

            if (option != null)
            {
                this.CacheOptions = new MSALMemoryTokenCacheOptions();
            }
            else
            {
                this.CacheOptions = option;
            }
        }
Esempio n. 2
0
        /// <summary>Initializes a new instance of the <see cref="MSALPerUserMemoryTokenCache"/> class.</summary>
        /// <param name="cache">The memory cache instance</param>
        public MSALPerUserMemoryTokenCacheProvider(IMemoryCache cache, MSALMemoryTokenCacheOptions option, IHttpContextAccessor httpContextAccessor)
        {
            this.memoryCache         = cache;
            this.httpContextAccessor = httpContextAccessor;

            if (option != null)
            {
                this.CacheOptions = new MSALMemoryTokenCacheOptions();
            }
            else
            {
                this.CacheOptions = option;
            }
        }
        public MSALAppMemoryTokenCacheProvider(IMemoryCache cache,
                                               MSALMemoryTokenCacheOptions option,
                                               IOptionsMonitor <AzureADOptions> azureAdOptionsAccessor)
        {
            if (option != null)
            {
                this.CacheOptions = new MSALMemoryTokenCacheOptions();
            }
            else
            {
                this.CacheOptions = option;
            }

            if (azureAdOptionsAccessor.CurrentValue == null && string.IsNullOrWhiteSpace(azureAdOptionsAccessor.CurrentValue.ClientId))
            {
                throw new ArgumentNullException(nameof(AzureADOptions), $"The app token cache needs {nameof(AzureADOptions)}, populated with clientId to initialize.");
            }

            this.AppId       = azureAdOptionsAccessor.CurrentValue.ClientId;
            this.memoryCache = cache;
        }
Esempio n. 4
0
        /// <summary>Adds the in-memory based per user token cache to the service collection.</summary>
        /// <param name="services">The services collection to add to.</param>
        /// <param name="cacheOptions">the MSALMemoryTokenCacheOptions allows the caller to set the token cache expiration</param>
        /// <returns></returns>
        public static IServiceCollection AddInMemoryPerUserTokenCache(this IServiceCollection services, MSALMemoryTokenCacheOptions cacheOptions)
        {
            services.AddMemoryCache();

            services.AddSingleton <IMSALUserTokenCacheProvider>(factory =>
            {
                var memoryCache = factory.GetRequiredService <IMemoryCache>();
                return(new MSALPerUserMemoryTokenCacheProvider(memoryCache, cacheOptions));
            });

            return(services);
        }
Esempio n. 5
0
        /// <summary>Adds the in-memory based application token cache to the service collection.</summary>
        /// <param name="services">The services collection to add to.</param>
        /// <param name="cacheOptions">the MSALMemoryTokenCacheOptions allows the caller to set the token cache expiration</param>
        public static IServiceCollection AddInMemoryAppTokenCache(this IServiceCollection services, MSALMemoryTokenCacheOptions cacheOptions)
        {
            services.AddMemoryCache();

            services.AddSingleton <IMSALAppTokenCacheProvider>(factory =>
            {
                var memoryCache    = factory.GetRequiredService <IMemoryCache>();
                var optionsMonitor = factory.GetRequiredService <IOptionsMonitor <AzureADOptions> >();

                return(new MSALAppMemoryTokenCacheProvider(memoryCache, cacheOptions, optionsMonitor));
            });

            return(services);
        }
Esempio n. 6
0
        /// <summary>Adds both the app and per-user in-memory token caches.</summary>
        /// <param name="services">The services collection to add to.</param>
        /// <param name="cacheOptions">the MSALMemoryTokenCacheOptions allows the caller to set the token cache expiration</param>
        /// <returns></returns>
        public static IServiceCollection AddInMemoryTokenCaches(this IServiceCollection services, MSALMemoryTokenCacheOptions cacheOptions = null)
        {
            var memoryCacheoptions = (cacheOptions == null) ? new MSALMemoryTokenCacheOptions {
                AbsoluteExpiration = DateTimeOffset.Now.AddDays(14)
            }
            : cacheOptions;

            AddInMemoryAppTokenCache(services, memoryCacheoptions);
            AddInMemoryPerUserTokenCache(services, memoryCacheoptions);
            return(services);
        }
Esempio n. 7
0
        /// <summary>Adds both the app and per-user in-memory token caches.</summary>
        /// <param name="services">The services collection to add to.</param>
        /// <param name="cacheOptions">the MSALMemoryTokenCacheOptions allows the caller to set the token cache expiration.</param>
        /// <returns></returns>
        public static IServiceCollection AddInMemoryTokenCaches(this IServiceCollection services, MSALMemoryTokenCacheOptions cacheOptions = null)
        {
            var memoryCacheoptions = (cacheOptions == null) ? new MSALMemoryTokenCacheOptions {
                SlidingExpiration = TimeSpan.FromDays(14)
            }
            : cacheOptions;

            AddInMemoryAppTokenCache(services, memoryCacheoptions);
            AddInMemoryPerUserTokenCache(services, memoryCacheoptions);
            return(services);
        }