// This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            Settings settings = new Settings(Configuration);

            services.AddSingleton <IDalSettings>(settings);

            DalModule.Register(services);
            BlModule.Register(services);

            services
            .AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultSignInScheme       = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = CookieAuthenticationDefaults.AuthenticationScheme;
            })
            .AddCookie(options =>
            {
                options.LoginPath  = "/api/account/LogIn";
                options.LogoutPath = "/api/account/LogOff";
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "wwwroot";
            });

            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
        }
Example #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            Settings settings = new Settings(Configuration);

            services.AddSingleton <IDalSettings>(settings);
            services.AddSingleton <IWebApiSettings>(settings);

            DalModule.Register(services);
            BlModule.Register(services);
            WebApiModule.Register(services);

            Mapper.Initialize(config =>
            {
                DalMappings.Initialize(config);
                BlMappings.Initialize(config);
                WebApiMappings.Initialize(config);
            });

            // ===== Add Jwt Authentication ========
            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); // => remove default claims

            services
            .AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultScheme             = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(cfg =>
            {
                cfg.RequireHttpsMetadata = false;
                cfg.SaveToken            = true;

                cfg.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidIssuer      = settings.JwtIssuer,
                    ValidAudience    = settings.JwtIssuer,
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(settings.JwtKey)),
                    ClockSkew        = TimeSpan.Zero      // remove delay of token when expire
                };
            });

            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy",
                                  builder => builder.AllowAnyOrigin()
                                  .AllowAnyMethod()
                                  .AllowAnyHeader()
                                  .AllowCredentials());
            });

            services.AddMvc();

            services.AddSingleton <BackgroundJobsRegistry>();
        }
Example #3
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton <IDalSettings, DalSettings>();
            services.AddSingleton <IBookingSettings, BookingSettings>();
            services.AddTransient <IUserInfo, UserInfo>();
            services.AddSingleton <IAccountUpdatingSettings, AccountUpdatingSettings>();
            services.AddSingleton <IProfileCachingSettings, ProfileCachingSettings>();

            FrontendFilesSettings frontendFilesSettings = new FrontendFilesSettings(Configuration);

            services.AddSingleton <IFrontendFilesSettings>(frontendFilesSettings);

            FilesUploadingSettings filesUploadingSettings = new FilesUploadingSettings(Configuration);

            services.AddSingleton <IFilesUploadingSettings, FilesUploadingSettings>();
            services.AddSingleton <IPaginationSettings, PaginationSettings>();

            CorsSettings corsSettings = new CorsSettings(Configuration);

            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy",
                                  builder =>
                {
                    builder.WithOrigins(corsSettings.AllowedOrigins)
                    .AllowAnyHeader()
                    .AllowAnyMethod();
                });
            });

            MapperConfiguration mappingConfig = new MapperConfiguration(config =>
            {
                WebAPIMapping.Initialize(config);
                BlMapping.Initialize(config);
                DalMapping.Initialize(config);
            });

            mappingConfig.CompileMappings();

            services.AddSingleton <IMapper>(mappingConfig.CreateMapper());

            services.AddControllers();

            DalModule.Register(services);
            BlModule.Register(services);
            WebAPIModule.Register(services);

            Serilog.ILogger logger = new LoggerConfiguration()
                                     .ReadFrom.Configuration(Configuration)
                                     .CreateLogger();

            services.AddLogging((builder) =>
            {
                builder.AddSerilog(logger, dispose: true);
            });

            JwtSettings jwtSettings = new JwtSettings(Configuration);

            services.AddSingleton <IJwtSettings>(jwtSettings);

            byte[] key = Encoding.UTF8.GetBytes(jwtSettings.Secret);
            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(x =>
            {
                x.RequireHttpsMetadata      = false;
                x.SaveToken                 = false;
                x.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(key),
                    ValidateIssuer           = true,
                    ValidIssuer      = jwtSettings.Issuer,
                    ValidateAudience = true,
                    ValidAudiences   = corsSettings.AllowedOrigins
                };
            });

            services.AddHttpContextAccessor();

            services.Configure <FormOptions>(options =>
            {
                // converting to bytes
                options.MultipartBodyLengthLimit = filesUploadingSettings.MaxMbSize * 1024 * 1024;
            });

            services.AddMemoryCache();

            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = Path.Combine(frontendFilesSettings.StoragePath);
            });
        }