Beispiel #1
0
        /// <summary>
        /// Configure services.
        /// </summary>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <OMDbScrubberContext>(options => options.UseSqlServer(
                                                            configuration.GetConnectionString("DefaultConnection")));

            services.AddScoped <IMoviesService, EFCoreMoviesService>();

            var jsonFileMessageRepository = new JsonFileMessageRepository(Environment.CurrentDirectory + @"\Messages");
            var repositoryMiddleware      = new RepositoryMiddleware(jsonFileMessageRepository);

            var pipelineContainer = new DefaultMessagePipelineContainer();

            services.AddSingleton <IMessagePipelineContainer>(pipelineContainer);
            services.AddScoped <IMessagePipelineService, DefaultMessagePipelineService>();
            pipelineContainer.AddCommandPipeline().AddStandardMiddlewares(o => o.SetAssemblies(typeof(IMoviesService).Assembly)).AddMiddleware(repositoryMiddleware);
            pipelineContainer.AddQueryPipeline().AddStandardMiddlewares().AddMiddleware(repositoryMiddleware);

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }
Beispiel #2
0
        /// <summary>
        /// Configure services.
        /// </summary>
        /// <param name="services">Service collection.</param>
        public void ConfigureServices(IServiceCollection services)
        {
            // DB services.
            services.AddDbContext <EstateDbContext>(options => options.UseSqlServer(
                                                        Configuration.GetConnectionString("UnrealEstateDB")));

            // Generators services.
            services.AddScoped <ITokenGenerator, JwtTokenGenerator>();

            // Repositories services.
            services.AddScoped <IUserRepository, UserRepository>();
            services.AddScoped <IListingRepository, ListingRepository>();
            services.AddScoped <ICommentRepository, CommentRepository>();

            // Pipeline services.
            var jsonFileMessageRepository = new JsonFileMessageRepository(Environment.CurrentDirectory + @"\Messages");
            var repositoryMiddleware      = new RepositoryMiddleware(jsonFileMessageRepository);

            var pipelineContainer = new DefaultMessagePipelineContainer();

            services.AddSingleton <IMessagePipelineContainer>(pipelineContainer);
            services.AddScoped <IMessagePipelineService, DefaultMessagePipelineService>();
            pipelineContainer.AddCommandPipeline()
            .AddStandardMiddlewares(o => o.SetAssemblies(typeof(IUserRepository).Assembly))
            .AddMiddleware(repositoryMiddleware);
            pipelineContainer.AddQueryPipeline().AddStandardMiddlewares().AddMiddleware(repositoryMiddleware);

            // Add Identity.
            services.AddIdentity <User, IdentityRole>(
                options =>
            {
                options.Password.RequiredLength         = 8;
                options.Password.RequireDigit           = true;
                options.Password.RequireLowercase       = true;
                options.Password.RequireUppercase       = true;
                options.Password.RequiredUniqueChars    = default;
                options.Password.RequireNonAlphanumeric = false;
            })
            .AddEntityFrameworkStores <EstateDbContext>()
            .AddDefaultUI()
            .AddDefaultTokenProviders();

            // Add authentication.
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.RequireHttpsMetadata      = true;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer = true,
                    ValidIssuer    = AuthOptions.Issuer,

                    ValidateAudience = true,
                    ValidAudience    = AuthOptions.Audience,
                    ValidateLifetime = true,

                    IssuerSigningKey         = AuthOptions.GetSymmetricSecurityKey(),
                    ValidateIssuerSigningKey = true
                };
            });

            services.AddAuthorization(options =>
            {
                options.DefaultPolicy = new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme)
                                        .RequireAuthenticatedUser()
                                        .Build();
            });

            var mappingConfig = new MapperConfiguration(mc =>
            {
                mc.AddProfiles(new List <Profile> {
                    new DtoMappingProfile(), new CommandMappingProfile()
                });
            });

            IMapper mapper = mappingConfig.CreateMapper();

            services.AddSingleton(mapper);

            // Add swagger.
            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new Info
                {
                    Version        = "v1",
                    Title          = "Unreal Estate API",
                    TermsOfService = "None"
                });

                options.AddSecurityDefinition("Bearer", new ApiKeyScheme
                {
                    In          = "header",
                    Name        = "Authorization",
                    Description = "JWT Authorization header",
                    Type        = "apiKey"
                });

                options.AddSecurityRequirement(new Dictionary <string, IEnumerable <string> >
                {
                    { "Bearer", new string[] { } }
                });

                var xmlPath = Path.Combine(AppContext.BaseDirectory, $"{Assembly.GetExecutingAssembly().GetName().Name}.xml");
                options.IncludeXmlComments(xmlPath);
            });

            // Add MVC.
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }