Пример #1
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddAutoMapper(typeof(Model.Mappings.AutoMapperAssemblyPointer).Assembly);

            #region Swagger

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info {
                    Title = "NIIS API", Version = "v1"
                });
            });

            #endregion

            #region Глобальные зависимости
            //services
            //.AddDbContextPool<NiisWebContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
            //                                                                    sqlServerOptions => sqlServerOptions.CommandTimeout(180)), poolSize: 128)
            services.AddDbContext <NiisWebContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
                                                                                   sqlServerOptions => sqlServerOptions.CommandTimeout(300)))

            .AddTransient <DbContext, NiisWebContext>()
            //.AddTransient<IExecutor, Executor>()
            .AddTransient <IExecutor, NiisRepository>()
            //.AddScoped<IExecutor, NiisRepository>()
            .AddTransient <IAmbientContext, AmbientContext>()
            .AddTransient <IUnitOfWork, UnitOfWork>()
            .AddTransient <IObjectResolver, ObjectResolver>();


            #endregion

            #region Локальные зависимости проекта

            //TODO: необходимо распределить корректно зависимости. Сейчас слишком много мест.
            services
            .AddCommonTypeServiceCollection();

            services
            .AddNiisAuthenticationServiceDependencies()
            .AddNiisBusinessLogicDependencies()     // Add all Iserv.Niis.BusinessLogic dependencies
            .AddNiisDataBridgeDependencies()        // Add all  Iserv.Niis.DataBridge dependencies
            .AddNiisWorkFlowBusinessLogicDependencies()
            .AddNiisReportBusinessLogicDependencies()
            .AddNiisDIDependencies()
            .AddNiisServicesDependencies();


            services.AddTransient <IAutoRouteStageHelper, AutoRouteStageHelper>();

            services.AddWorkflowServices();

            services.Configure <ConfigExternalService>(opt => Configuration.GetSection("ConfigExternalService").Bind(opt));
            services.AddTransient <IIntegrationOneCApiClient, IntegrationOneCApiClient>();

            #endregion

            #region Authoriazation settings

            services.AddScoped <IUserClaimsPrincipalFactory <ApplicationUser>, AppClaimsPrincipalFactory>();
            services.AddTransient <IJwtFactory, JwtFactory>();

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options => ConfigurationOptions.JwtBearerOptions(options, Configuration))
            .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options => options.SessionStore = new MemoryCacheTicketStore());

            services.AddIdentity <ApplicationUser, ApplicationRole>(ConfigurationOptions.IdentityOptions).AddEntityFrameworkStores <NiisWebContext>();

            services.AddMvc(options =>
            {
                options.Filters.Add(typeof(GlobalExceptionFilter));

                var authorizePolicy = new AuthorizationPolicyBuilder()
                                      .RequireAuthenticatedUser()
                                      .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
                                      .Build();
                options.Filters.Add(new AuthorizeFilter(authorizePolicy));
            })
            .AddSessionStateTempDataProvider();

            services.AddAuthorization(options =>
            {
                const string prmName = KeyFor.JwtClaimIdentifier.Permission;

                options.AddPolicy(KeyFor.Policy.HasAccessToJournal, policy => policy.RequireClaim(prmName, KeyFor.Permission.JournalModule));
                options.AddPolicy(KeyFor.Policy.HasAccessToViewStaffTasks, policy => policy.RequireClaim(prmName, KeyFor.Permission.JournalViewStaffTasks));
                options.AddPolicy(KeyFor.Policy.HasAccessToAdministration, policy => policy.RequireClaim(prmName, KeyFor.Permission.AdministrationModule));
            });

            #endregion

            #region Hosted services

            services.AddHostedService <ImportPaymentsFrom1CHostedService>();

            #endregion

            //Создается экземпляр в конце этого метода
            var _   = new AmbientContext(services.BuildServiceProvider());
            var __  = new NiisAmbientContext(services.BuildServiceProvider(), Configuration);
            var ___ = new NiisWorkflowAmbientContext(services.BuildServiceProvider());
        }
Пример #2
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton <IConfiguration>(Configuration);

            #region Отрефакторенные зависимости

            services
            .AddCommonTypesToServiceCollection();

            services.AddTransient <IUserClaimsPrincipalFactory <ApplicationUser>, AppClaimsPrincipalFactory>();
            services.AddTransient <IJwtFactory, JwtFactory>();

            #endregion

            services.AddMvc(options =>
            {
                options.Filters.Add(typeof(GlobalExceptionFilter));
                options.DisableAnonimousAccess();
            });

            services.AddAutoMapperConfiguration();
            services.AddDbContext <NiisWebContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
            services.AddIdentity <ApplicationUser, ApplicationRole>(ConfigurationOptions.IdentityOptions).AddEntityFrameworkStores <NiisWebContext>();
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options => ConfigurationOptions.JwtBearerOptions(options, Configuration));
            services.AddAuthorizationPolicies();
            services.AddCors();
            services.Configure <ConfigExternalService>(opt => Configuration.GetSection("ConfigExternalService").Bind(opt));
            services.AddResponseCompression();

            services.Configure <FormOptions>(o =>
            {
                o.ValueLengthLimit         = int.MaxValue;
                o.MultipartBodyLengthLimit = int.MaxValue;
                o.MemoryBufferThreshold    = int.MaxValue;
                o.MultipartBodyLengthLimit = int.MaxValue;
            });
        }
Пример #3
0
 private static void RegisterAuthoriazation(IServiceCollection serviceCollection, IConfiguration configuration)
 {
     serviceCollection.AddScoped <IUserClaimsPrincipalFactory <ApplicationUser>, AppClaimsPrincipalFactory>();
     serviceCollection.AddTransient <IJwtFactory, JwtFactory>();
     serviceCollection.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options => ConfigurationOptions.JwtBearerOptions(options, configuration));
     serviceCollection.AddIdentity <ApplicationUser, ApplicationRole>(ConfigurationOptions.IdentityOptions).AddEntityFrameworkStores <NiisWebContextMigration>();
 }