Esempio n. 1
0
        /// <summary>Adds the Sql Server based per user token cache to the service collection.</summary>
        /// <param name="services">The services collection to add to.</param>
        /// <param name="sqlTokenCacheOptions">The MSALSqlTokenCacheOptions is used by the caller to specify the Sql connection string</param>
        /// <returns></returns>
        public static IServiceCollection AddSqlPerUserTokenCache(this IServiceCollection services, MSALSqlTokenCacheOptions sqlTokenCacheOptions)
        {
            // Uncomment the following lines to create the database. In production scenarios, the database
            // will most probably be already present.

            /*
             * var tokenCacheDbContextBuilder = new DbContextOptionsBuilder<TokenCacheDbContext>();
             * tokenCacheDbContextBuilder.UseSqlServer(sqlTokenCacheOptions.SqlConnectionString);
             *
             * var tokenCacheDbContextForCreation = new TokenCacheDbContext(tokenCacheDbContextBuilder.Options);
             * tokenCacheDbContextForCreation.Database.EnsureCreated();
             */

            // To share protected payloads among apps, configure SetApplicationName in each app with the same value.
            // https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/overview?view=aspnetcore-2.2#setapplicationname
            services.AddDataProtection()
            .SetApplicationName("WebApp_Tutorial");

            services.AddDbContext <TokenCacheDbContext>(options =>
                                                        options.UseSqlServer(sqlTokenCacheOptions.SqlConnectionString));

            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            services.AddScoped <IMSALUserTokenCacheProvider>(factory =>
            {
                var dpprovider          = factory.GetRequiredService <IDataProtectionProvider>();
                var tokenCacheDbContext = factory.GetRequiredService <TokenCacheDbContext>();
                var httpcontext         = factory.GetRequiredService <IHttpContextAccessor>();

                return(new MSALPerUserSqlTokenCacheProvider(tokenCacheDbContext, dpprovider, httpcontext));
            });

            return(services);
        }
Esempio n. 2
0
        /// <summary>Adds the Sql Server based application token cache to the service collection.</summary>
        /// <param name="services">The services collection to add to.</param>
        /// <param name="sqlTokenCacheOptions">The MSALSqlTokenCacheOptions is used by the caller to specify the Sql connection string</param>
        /// <returns></returns>
        public static IServiceCollection AddSqlAppTokenCache(this IServiceCollection services, MSALSqlTokenCacheOptions sqlTokenCacheOptions)
        {
            // Uncomment the following lines to create the database. In production scenarios, the database
            // will most probably be already present.

/*
 *          var tokenCacheDbContextBuilder = new DbContextOptionsBuilder<TokenCacheDbContext>();
 *          tokenCacheDbContextBuilder.UseSqlServer(sqlTokenCacheOptions.SqlConnectionString);
 *
 *          var tokenCacheDbContextForCreation = new TokenCacheDbContext(tokenCacheDbContextBuilder.Options);
 *          tokenCacheDbContextForCreation.Database.EnsureCreated();
 */
            services.AddDataProtection();

            services.AddDbContext <TokenCacheDbContext>(options =>
                                                        options.UseSqlServer(sqlTokenCacheOptions.SqlConnectionString));

            services.AddScoped <IMSALAppTokenCacheProvider>(factory =>
            {
                var dpprovider          = factory.GetRequiredService <IDataProtectionProvider>();
                var tokenCacheDbContext = factory.GetRequiredService <TokenCacheDbContext>();
                var optionsMonitor      = factory.GetRequiredService <IOptionsMonitor <AzureADOptions> >();

                return(new MSALAppSqlTokenCacheProvider(tokenCacheDbContext, optionsMonitor, dpprovider));
            });

            return(services);
        }
Esempio n. 3
0
 /// <summary>Adds the app and per user SQL token caches.</summary>
 /// <param name="configuration">The configuration instance from where this method pulls the connection string to the Sql database.</param>
 /// <param name="sqlTokenCacheOptions">The MSALSqlTokenCacheOptions is used by the caller to specify the Sql connection string</param>
 /// <returns></returns>
 public static IServiceCollection AddSqlTokenCaches(this IServiceCollection services, MSALSqlTokenCacheOptions sqlTokenCacheOptions)
 {
     AddSqlAppTokenCache(services, sqlTokenCacheOptions);
     AddSqlPerUserTokenCache(services, sqlTokenCacheOptions);
     return(services);
 }
        /// <summary>Adds the Sql Server based per user token cache to the service collection.</summary>
        /// <param name="services">The services collection to add to.</param>
        /// <param name="sqlTokenCacheOptions">The MSALSqlTokenCacheOptions is used by the caller to specify the Sql connection string</param>
        /// <returns></returns>
        public static IServiceCollection AddSqlPerUserTokenCache(this IServiceCollection services, MSALSqlTokenCacheOptions sqlTokenCacheOptions)
        {
            // Uncomment the following lines to create the database. In production scenarios, the database
            // will most probably be already present.
            //var tokenCacheDbContextBuilder = new DbContextOptionsBuilder<TokenCacheDbContext>();
            //tokenCacheDbContextBuilder.UseSqlServer(configuration.GetConnectionString("TokenCacheDbConnStr"));

            //var tokenCacheDbContext = new TokenCacheDbContext(tokenCacheDbContextBuilder.Options);
            //tokenCacheDbContext.Database.EnsureCreated();

            services.AddDataProtection();

            services.AddDbContext <TokenCacheDbContext>(options =>
                                                        options.UseSqlServer(sqlTokenCacheOptions.SqlConnectionString));

            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            services.AddScoped <IMSALUserTokenCacheProvider>(factory =>
            {
                var dpprovider          = factory.GetRequiredService <IDataProtectionProvider>();
                var tokenCacheDbContext = factory.GetRequiredService <TokenCacheDbContext>();
                var httpcontext         = factory.GetRequiredService <IHttpContextAccessor>();

                return(new MSALPerUserSqlTokenCacheProvider(tokenCacheDbContext, dpprovider, httpcontext));
            });

            return(services);
        }
Esempio n. 5
0
        /// <summary>Adds the Sql Server based per user token cache to the service collection.</summary>
        /// <param name="services">The services collection to add to.</param>
        /// <param name="sqlTokenCacheOptions">The MSALSqlTokenCacheOptions is used by the caller to specify the Sql connection string</param>
        /// <returns></returns>
        public static IServiceCollection AddSqlPerUserTokenCache(this IServiceCollection services, MSALSqlTokenCacheOptions sqlTokenCacheOptions)
        {
            services.AddDataProtection();

            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            services.AddScoped <IMSALUserTokenCacheProvider>(factory =>
            {
                var dpprovider          = factory.GetRequiredService <IDataProtectionProvider>();
                var tokenCacheDbContext = (ITokenCacheDbContext)factory.GetRequiredService(sqlTokenCacheOptions.ContextType);
                var httpcontext         = factory.GetRequiredService <IHttpContextAccessor>();

                return(new MSALPerUserSqlTokenCacheProvider(tokenCacheDbContext, dpprovider, httpcontext));
            });

            return(services);
        }
Esempio n. 6
0
        /// <summary>Adds the Sql Server based application token cache to the service collection.</summary>
        /// <param name="services">The services collection to add to.</param>
        /// <param name="sqlTokenCacheOptions">The MSALSqlTokenCacheOptions is used by the caller to specify the Sql connection string</param>
        /// <returns></returns>
        public static IServiceCollection AddSqlAppTokenCache(this IServiceCollection services, MSALSqlTokenCacheOptions sqlTokenCacheOptions)
        {
            services.AddDataProtection();

            services.AddScoped <IMSALAppTokenCacheProvider>(factory =>
            {
                var dpprovider          = factory.GetRequiredService <IDataProtectionProvider>();
                var tokenCacheDbContext = (ITokenCacheDbContext)factory.GetRequiredService(sqlTokenCacheOptions.ContextType);
                var optionsMonitor      = factory.GetRequiredService <IOptionsMonitor <AzureADOptions> >();

                return(new MSALAppSqlTokenCacheProvider(tokenCacheDbContext, optionsMonitor, dpprovider));
            });

            return(services);
        }