Ejemplo n.º 1
0
        protected BaseTestClass()
        {
            var efServiceProvider = new ServiceCollection().AddEntityFrameworkInMemoryDatabase().BuildServiceProvider();
            var services          = new ServiceCollection();

            services.AddSingleton <IConfiguration>(new ConfigurationBuilder().Build());
            services.AddOptions();
            services.AddDbContext <WmipDbContext>(b => b.UseInMemoryDatabase("WmipDbContext").UseInternalServiceProvider(efServiceProvider));
            services.AddIdentity <User, IdentityRole>()
            .AddDefaultTokenProviders()
            .AddEntityFrameworkStores <WmipDbContext>();

            services.Configure <IdentityOptions>(options =>
            {
                options.SignIn.RequireConfirmedEmail    = PasswordConstants.RequireConfirmedEmail;
                options.Password.RequireLowercase       = PasswordConstants.RequireLowercase;
                options.Password.RequireUppercase       = PasswordConstants.RequireUppercase;
                options.Password.RequireNonAlphanumeric = PasswordConstants.RequireNonAlphanumeric;
                options.Password.RequiredLength         = PasswordConstants.RequiredLength;
                options.Password.RequiredUniqueChars    = PasswordConstants.RequiredUniqueChars;
                options.Password.RequireDigit           = PasswordConstants.RequireDigit;
            });

            services.Configure <SecurityStampValidatorOptions>(options =>
            {
                options.ValidationInterval = TimeSpan.Zero;
            });

            var httpContext = new DefaultHttpContext();

            services.AddSingleton <IHttpContextAccessor>(
                new HttpContextAccessor()
            {
                HttpContext = httpContext,
            });

            var mapperConfigBuilder = new MapperConfigBuilder();
            var config = mapperConfigBuilder.Execute(Assembly.GetEntryAssembly(), Assembly.GetAssembly(typeof(ISearchService)));

            this.Mapper = config.CreateMapper();

            this.ServiceProvider = services.BuildServiceProvider();
        }
Ejemplo n.º 2
0
        // 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;
            });

            // Configure db context
            services.AddDbContext <WmipDbContext>(options =>
                                                  options.UseLazyLoadingProxies()
                                                  .UseSqlServer(this.Configuration.GetConnectionString("DefaultConnection")));

            // Configure services
            services.AddTransient <IUsersService, UsersService>();
            services.AddTransient <IArticlesService, ArticlesService>();
            services.AddTransient <ISongsService, SongsService>();
            services.AddTransient <IApprovalService, ApprovalService>();
            services.AddTransient <IAlbumsService, AlbumsService>();
            services.AddTransient <IReviewsService, ReviewsService>();
            services.AddTransient <ICommentsService, CommentsService>();
            services.AddTransient <ISearchService, SearchService>();
            services.AddTransient <IRatingsService, RatingsService>();

            // Configure AutoMapper
            var mapperConfigBuilder = new MapperConfigBuilder();
            var config = mapperConfigBuilder.Execute(Assembly.GetExecutingAssembly(), Assembly.GetAssembly(typeof(ISearchService)));
            var mapper = config.CreateMapper();

            services.AddSingleton(mapper);

            // Change password requirements
            services.Configure <IdentityOptions>(options =>
            {
                options.SignIn.RequireConfirmedEmail    = PasswordConstants.RequireConfirmedEmail;
                options.Password.RequireLowercase       = PasswordConstants.RequireLowercase;
                options.Password.RequireUppercase       = PasswordConstants.RequireUppercase;
                options.Password.RequireNonAlphanumeric = PasswordConstants.RequireNonAlphanumeric;
                options.Password.RequiredLength         = PasswordConstants.RequiredLength;
                options.Password.RequiredUniqueChars    = PasswordConstants.RequiredUniqueChars;
                options.Password.RequireDigit           = PasswordConstants.RequireDigit;
            });

            // Configure identity
            services.AddIdentity <User, IdentityRole>()
            .AddDefaultTokenProviders()
            .AddEntityFrameworkStores <WmipDbContext>();

            services.ConfigureApplicationCookie(options =>
            {
                options.LoginPath        = new PathString("/Users/Login");
                options.LogoutPath       = new PathString("/Users/Logout");
                options.AccessDeniedPath = new PathString("/Users/AccessDenied");
            });

            services.Configure <SecurityStampValidatorOptions>(options =>
            {
                options.ValidationInterval = TimeSpan.Zero;
            });

            services.AddMvc(options =>
            {
                options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());
            })
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }