コード例 #1
0
        /// <summary>
        /// Service Configurator Method
        /// </summary>
        /// <param name="services">Services</param>
        // ReSharper disable once UnusedMember.Global
        public void ConfigureServices(IServiceCollection services)
        {
            ConfigureAutoMapper();
            services.Configure <DbMandatoryFields>(x => Configuration.GetSection(nameof(DbMandatoryFields)).Bind(x));
            services.AddScoped(cfg => cfg.GetService <IOptionsSnapshot <DbMandatoryFields> >().Value);
            services.Configure <LocalizationSettings>(x =>
                                                      Configuration.GetSection(nameof(LocalizationSettings)).Bind(x));
            services.AddScoped(cfg => cfg.GetService <IOptionsSnapshot <LocalizationSettings> >().Value);
            services.Configure <StatUnitAnalysisRules>(x =>
                                                       Configuration.GetSection(nameof(StatUnitAnalysisRules)).Bind(x));
            services.AddScoped(cfg => cfg.GetService <IOptionsSnapshot <StatUnitAnalysisRules> >().Value);
            services.Configure <ServicesSettings>(x => Configuration.GetSection(nameof(ServicesSettings)).Bind(x));
            services.AddScoped(cfg => cfg.GetService <IOptionsSnapshot <ServicesSettings> >().Value);
            services.Configure <ReportingSettings>(x => Configuration.GetSection(nameof(ReportingSettings)).Bind(x));
            services.AddScoped(cfg => cfg.GetService <IOptionsSnapshot <ReportingSettings> >().Value);
            services.Configure <ValidationSettings>(Configuration.GetSection(nameof(ValidationSettings)));
            services.AddScoped(cfg => cfg.GetService <IOptionsSnapshot <ValidationSettings> >().Value);
            services
            .AddAntiforgery(op => op.Cookie.Name = op.HeaderName = "X-XSRF-TOKEN")
            .AddDbContext <NSCRegDbContext>(DbContextHelper.ConfigureOptions(Configuration))
            .AddIdentity <User, Role>(ConfigureIdentity)
            .AddEntityFrameworkStores <NSCRegDbContext>()
            .AddDefaultTokenProviders();
            services
            .AddScoped <IAuthorizationHandler, SystemFunctionAuthHandler>()
            .AddScoped <IUserService, UserService>();
            services.AddTransient(config => Configuration);
            services
            .AddMvcCore(op =>
            {
                op.Filters.Add(
                    new AuthorizeFilter(
                        new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()));
                op.Filters.Add(new ValidateModelStateAttribute());
            })
            .AddMvcOptions(op => op.Filters.Add(new GlobalExceptionFilter(_loggerFactory)))
            .AddFluentValidation(op => op.RegisterValidatorsFromAssemblyContaining <IStatUnitM>())
            .AddAuthorization(options => options.AddPolicy(
                                  nameof(SystemFunctions),
                                  policyBuilder => { policyBuilder.Requirements.Add(new SystemFunctionAuthRequirement()); }))
            .AddJsonFormatters(op => op.ContractResolver = new CamelCasePropertyNamesContractResolver())
            .AddRazorViewEngine()
            .AddDataAnnotationsLocalization()
            .AddViewLocalization()
            .AddViews();

            var keysDirectory = new DirectoryInfo(Configuration["DataProtectionKeysDir"]);

            if (!keysDirectory.Exists)
            {
                keysDirectory.Create();
            }

            services.AddDataProtection()
            .PersistKeysToFileSystem(keysDirectory)
            .SetApplicationName("nscreg")
            .SetDefaultKeyLifetime(TimeSpan.FromDays(7));
        }