private PermissionDataContext GetDbContext()
        {
            var myLoggerFactory = new LoggerFactory(new[] {
                new ConsoleLoggerProvider((_, __) =>
                {
                    return(true);
                }, true)
            });
            var optionsBuilder = new DbContextOptionsBuilder <PermissionDataContext>();

            optionsBuilder.UseLoggerFactory(myLoggerFactory)
            .EnableSensitiveDataLogging()
            .UseSqlServer(connectionString);
            var dataContext = new PermissionDataContext(optionsBuilder.Options);

            return(dataContext);
        }
        /// <summary>
        /// This method gets called by the runtime. Use this method to add services to the container.
        /// </summary>
        /// <param name="services"></param>
        public void ConfigureServices(IServiceCollection services)
        {
            var appSettings  = this.Configuration.GetSection("AppSettings");
            var settingModel = appSettings.Get <SpConfiguration>();

            services.AddSingleton(settingModel);

            // Auto inject business classes
            services.Scan(scan => scan
                          .FromAssemblyOf <ManagerBase>()
                          .AddClasses(classes => classes.Where(c => c.Name.EndsWith("Manager")))
                          .AsSelf()
                          .WithTransientLifetime());

            // Auto inject dao classes
            services.Scan(scan => scan
                          .FromAssemblyOf <DaoBase>()
                          .AddClasses(classes => classes.Where(c => c.Name.EndsWith("Dao")))
                          .AsSelf()
                          .WithTransientLifetime());

            // Register component
            services.AddSingleton <CommonRedisHelper>(new CommonRedisHelper(settingModel.CommonRedis, settingModel.RedisPrefix));

            // Token handler, The default is save token and associate data in memoery.
            services.AddSingleton <ITokenHandler>(new MemoryTokenHandler());
            // You can use redis as token storage
            // services.AddSingleton<ITokenHandler>(new RedisTokenHandler(new PermissionRedisHelper(settingModel.PermissionRedis, settingModel.RedisPrefix), "token"));

            // It will initial in SpAuthFilter, It simplify the reference of request object
            // It includes token, associate object of token, token handler instance etc.
            services.AddScoped <BizContext>();
            services.AddScoped <DaoContext>();

            services.AddDbContext <PermissionDataContext>
                (options =>
                options.UseSqlServer(Configuration.GetConnectionString("SpFrameworkDatabase"), b => b.MigrationsAssembly("SuperPartner"))
                .UseLoggerFactory(MyLoggerFactory)
                );
            services.AddDbContext <SpFrameworkContext>
                (options =>
                options.UseSqlServer(Configuration.GetConnectionString("SpFrameworkDatabase"))
                .UseLoggerFactory(MyLoggerFactory)
                );

            // Permission implement
            // The inject will be use for UI
            services.AddScoped(typeof(IAuthorizationStorageProvider), typeof(AuthorizationStorageProvider));
            var optionsBuilder = new DbContextOptionsBuilder <PermissionDataContext>();

            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("SpFrameworkDatabase"));
            var dataContext          = new PermissionDataContext(optionsBuilder.Options);
            var authorizationHandler = new AuthorizationHandler(new AuthorizationStorageProvider(dataContext));

            // User cache to save performance. But the ClearCache must invoke if any change in AuthorizationStorageProvider
            authorizationHandler.UseCache = true;
            // It should be define as singleton
            services.AddSingleton(typeof(IAuthorizationHandler), authorizationHandler);

            services.AddControllers(options =>
            {
                options.Filters.Add(new SpExceptionFilter());
                options.Filters.Add(new SpAuthFilter());
            });

            // swagger configuration
            SwaggerSetting.Confige(services);
        }
示例#3
0
 public AuthorizationStorageProvider(PermissionDataContext dataContext)
 {
     this.dataContext = dataContext;
 }