// 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);
        }
            public async Task <Unit> Handle(Execute request, CancellationToken cancellationToken)
            {
                var sessioncart = new SessionShoppingCart
                {
                    CreateDate = request.CreateDateSession
                };

                _context.sessionShoppingCarts.Add(sessioncart);
                var value = await _context.SaveChangesAsync();

                if (value <= 0)
                {
                    throw new Exception("It was occured an error, creating the session");
                }

                int id = sessioncart.SessionShopingCartId;


                foreach (var product in request.Products)
                {
                    var sessionCartDetail = new SessionShoppingCartDetail
                    {
                        SessionShopingCartId = id,
                        ProductSelected      = product,
                        CreateDate           = DateTime.Now
                    };

                    _context.sessionShoppingCartDetails.Add(sessionCartDetail);
                }

                value = await _context.SaveChangesAsync();

                if (value <= 0)
                {
                    throw new Exception("It was occurd an error, to create your shopping cart");
                }

                return(Unit.Value);
            }
示例#3
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();
        }
 public ShoppingCartContextUnitTest()
 {
     SessionShoppingCart = new SessionShoppingCart(Context.Object);
 }