Ejemplo n.º 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure <AdminOptions>(Configuration.GetSection(nameof(AdminOptions)));

            services.AddControllers();
            // Register the Swagger generator, defining 1 or more Swagger documents
            services.AddSwaggerGen();

            services.AddSingleton(s => MapperConfig.ConfigureAutoMapper());
            services.AddHttpContextAccessor();
            services.RegisterRequestContext();
            services.AddSingleton <ITimeService, TimeService>();
            services.AddDbContext <RandomPairerDbContext>(o => o.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddScoped <IPairingService, PairingService>();
        }
Ejemplo n.º 2
0
        public UnitTests()
        {
            var services = new TestServiceBase();

            var serviceProvider = services.BuildServiceProvider();

            context = serviceProvider.GetService <OnlineStoreDbContext>();

            var accessMock = new AccessMock(context);

            var mapper = MapperConfig.ConfigureAutoMapper();

            var factory = serviceProvider.GetService <ILoggerFactory>();

            var logger = factory.CreateLogger <UserService>();

            var someOptions = Options.Create(new JwtOptions());

            var validator = new MockValidator();

            service = new UserService(null, null, null, someOptions, context, mapper, accessMock, logger, validator);

            var fileLogger = factory.CreateLogger <FileService>();

            fileService = new FileService(accessMock, context, mapper, fileLogger, validator);

            var testUser = new User
            {
                UserName    = "******",
                Email       = "*****@*****.**",
                PhoneNumber = "+36123456789",
            };

            if (context.ApplicationUsers.Count() < 1)
            {
                context.ApplicationUsers.Add(testUser);
            }

            context.SaveChanges();
        }
Ejemplo n.º 3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure <JwtOptions>(Configuration.GetSection(nameof(JwtOptions)));

            services.AddDbContext <OnlineStoreDbContext>(options =>
                                                         options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddDefaultIdentity <User>()
            .AddRoles <IdentityRole>()
            .AddEntityFrameworkStores <OnlineStoreDbContext>()
            .AddDefaultTokenProviders();


            services.AddControllers();

            services.AddMvc();
            services.AddSwaggerDocument();

            services.AddAuthentication(opt => {
                opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                opt.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
                opt.DefaultScheme             = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddCookie()
            .AddJwtBearer(jwtBearerOptions =>
            {
                jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidateActor            = false,
                    ValidateAudience         = false,
                    ValidateLifetime         = true,
                    SaveSigninToken          = true,
                    ValidateIssuerSigningKey = true,
                    ValidIssuer      = Configuration["JwtOptions:Issuer"],
                    ValidAudience    = Configuration["JwtOptions:Audience"],
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes
                                                                    (Configuration["JwtOptions:Key"]))
                };
            });

            services.Configure <IdentityOptions>(options =>
            {
                options.Password.RequireDigit           = false;
                options.Password.RequiredLength         = 6;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireUppercase       = false;
                options.Password.RequireLowercase       = false;
                options.Password.RequiredUniqueChars    = 1;
                options.Lockout.DefaultLockoutTimeSpan  = TimeSpan.FromMinutes(30);
                options.Lockout.MaxFailedAccessAttempts = 10;
                options.Lockout.AllowedForNewUsers      = true;
                options.User.RequireUniqueEmail         = true;
            });

            services.AddSingleton(s => MapperConfig.ConfigureAutoMapper());

            services.AddScoped <IModelValidator, ModelValidator>();

            services.AddTransient <IValidator <NewCommentModel>, CommentValidator>();
            services.AddTransient <IValidator <LoginModel>, LoginValidator>();
            services.AddTransient <IValidator <RegistrationModel>, RegisterValidator>();
            services.AddTransient <IValidator <UploadModel>, UploadValidator>();

            services.AddHttpContextAccessor();
            services.AddScoped <IUserService, UserService>();
            services.AddScoped <IUserAccess, UserAccess>();
            services.AddScoped <IFileService, FileService>();

            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "Client/dist";
            });
        }