Exemplo n.º 1
0
        public void ConfigureServices(IServiceCollection services)
        {
            // 使用分布式缓存
            // services.AddDistributedMemoryCache();

            // 启用Session
            services.AddSession();

            // 初始化数据库工具
            MyDbUtil.Init(Configuration);

            // 初始化并添加AutoMapper
            Mapper.Initialize(cfg =>
            {
                cfg.AddProfile <ServicesProfile>();
            });
            services.AddAutoMapper();

            // 压缩输出
            services.AddResponseCompression();

            // cookie身份验证
            services
            .AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(opts =>
            {
                opts.Cookie.Name     = "Taoxue.Training.Core";
                opts.Cookie.HttpOnly = true;
                opts.LoginPath       = "/Login";
                opts.LogoutPath      = "/Home/Logout";
            });
            services
            .AddAuthorization(opts =>
            {
                opts.AddPolicy("AdminOnly", policy => policy.RequireRole("admin"));
                opts.AddPolicy("ManagerOnly", policy => policy.RequireRole("manager"));
            });

            // senparc使用本地缓存必须注册此服务
            services.AddMemoryCache();

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }