// This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure <CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded    = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });



            //services.AddDbContext<MyContext>(options => options.UseSqlServer(Configuration["ConnectionString:MusicStoreCS"]));

            services.AddDbContext <MyContext>(options => {
                options.UseSqlServer(Configuration.GetConnectionString("MusicStore"));
            });
            services.AddDbContext <IdentityDbContext>(options =>
                                                      options.UseSqlServer(Configuration.GetConnectionString("MusicStoreCS")));

            services.AddIdentity <ApplicationUser, ApplicationUserRole>()
            .AddEntityFrameworkStores <IdentityDbContext>()
            .AddDefaultTokenProviders();

            //修改Identity配置
            services.Configure <IdentityOptions>(options =>
            {
                options.Password.RequireLowercase       = false; //小写字母
                options.Password.RequireNonAlphanumeric = false; //特殊符号
                options.Password.RequireUppercase       = false; //大写字母
                options.Password.RequiredLength         = 6;     //长度6位
            });


            // 注册SessionShoppingCart服务
            services.AddScoped <ShoppingCart>(sp => SessionShoppingCart.GetCart(sp));
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();


            //
            services.AddMemoryCache();
            services.AddSession();
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(options => {
                options.LoginPath = "/Account/Login";
            });
            services.AddAuthentication();
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }
Beispiel #2
0
        public void ConfigureProductionServices(IServiceCollection services)
        {
            services.AddDbContext <AppDbContext>(opts => opts.UseSqlite("Data Source=AppDb.sqlite"));
            services.AddDbContext <AppIdentityDbContext>(opts => opts.UseSqlite("Data Source=AppIdentity.sqlite"));

            services.AddTransient <IMenuRepository, EFMenuRepository>();
            services.AddTransient <IOrderCommunicationRepository, EFOrderCommunicationRepository>();

            services.AddTransient <IEmailService, EmailService>();
            services.AddScoped(provider => SessionShoppingCart.GetCart(provider));
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            services.AddIdentity <AppUser, IdentityRole>()
            .AddEntityFrameworkStores <AppIdentityDbContext>()
            .AddDefaultTokenProviders();

            services.AddMvc();

            // in memory--means that multiple servers will not work for this implementation.
            // if you want to scale this to multiple servers, this will need to be changed.
            // see https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-2.1&tabs=aspnetcore2x
            services.AddDistributedMemoryCache();
            services.AddSession();
        }