/// <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;
            }
        }
Exemplo n.º 2
0
        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;
        }
        /// <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.AddHttpContextAccessor();
            services.AddSingleton <IMSALUserTokenCacheProvider>(factory =>
            {
                var memoryCache = factory.GetRequiredService <IMemoryCache>();
                IHttpContextAccessor httpContextAccessor = factory.GetRequiredService <IHttpContextAccessor>();
                return(new MSALPerUserMemoryTokenCacheProvider(memoryCache, cacheOptions, httpContextAccessor));
            });

            return(services);
        }
        /// <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);
        }
        /// <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;

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