Beispiel #1
0
        // This method gets called by a runtime.
        // Use this method to add services to the container
        public void ConfigureServices(IServiceCollection services)
        {
            var    hostEnv        = services.BuildServiceProvider().GetRequiredService <IHostingEnvironment>();
            string configRootPath = services.BuildServiceProvider().GetRequiredService <IHostingEnvironment>().ConfigRootPath();

            //
            // Configuration
            services.AddSingleton((s) => Configuration);
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            //
            // Logging
            services.AddApiLogging();

            //
            // Auditing
            services.AddApiAuditing();

            //
            // Load plugins
            ModuleConfig modConfig = new ModuleConfig(Path.Combine(configRootPath, "modules.json"));
            ModuleLoader loader    = new ModuleLoader(hostEnv);

            LoadPlugins(loader, modConfig.Modules);

            services.AddOptions();


            //
            // CORS
            services.AddCors();

            //
            // Api Keys
            services.AddApiKeyProvider();

            //
            // Authentication
            services.AddAuthentication();
            services.AddAuthorization(o =>
            {
                o.AddPolicy("AccessToken", p => p.RequireAuthenticatedUser().RequireClaim(Core.Security.ClaimTypes.AccessToken));

                o.AddPolicy("Administrators", p => p.RequireAuthenticatedUser().RequireRole("Administrators"));

                o.AddPolicy("AdministrativeGroup", p => p.RequireAuthenticatedUser().RequireAssertion(authContext =>
                                                                                                      IsUserInAdministrators(authContext, Configuration)
                                                                                                      ));
            });

            //
            // Antiforgery
            services.TryAddSingleton <IAntiforgeryTokenStore, AntiForgeryTokenStore>();
            services.AddAntiforgery();
            services.AddAntiforgery(o =>
            {
                o.RequireSsl = true;
                o.CookieName = o.FormFieldName = Core.Http.HeaderNames.XSRF_TOKEN;
            });

            //
            // MVC
            IMvcBuilder builder = services.AddMvc(o => {
                // Replace default json output formatter
                o.OutputFormatters.RemoveType <AspNetCore.Mvc.Formatters.JsonOutputFormatter>();

                var settings = JsonSerializerSettingsProvider.CreateSerializerSettings();
                o.OutputFormatters.Add(new JsonOutputFormatter(settings, System.Buffers.ArrayPool <char> .Shared));

                // TODO
                // Workaround filter to fix Object Results returned from controllers
                // Remove when https://github.com/aspnet/Mvc/issues/4960 is resolved
                o.Filters.Add(typeof(Fix4960ActionFilter));

                o.Filters.Add(typeof(ResourceInfoFilter));

                RemoveFilter <UnsupportedContentTypeFilter>(o);
            });

            foreach (var asm in loader.GetAllLoadedAssemblies())
            {
                builder.AddApplicationPart(asm);
            }

            builder.AddControllersAsServices();
            builder.AddWebApiConventions();
        }
        // This method gets called by a runtime.
        // Use this method to add services to the container
        public void ConfigureServices(IServiceCollection services)
        {
            //
            // IHttpContextAccessor
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            //
            // Logging
            services.AddApiLogging();

            //
            // Auditing
            services.AddApiAuditing();

            //
            // Files
            services.AddFileProvider();

            //
            // Load plugins
            ModuleConfig modConfig = new ModuleConfig(_hostingEnv.GetConfigPath("modules.json"));
            ModuleLoader loader    = new ModuleLoader(_hostingEnv);

            LoadPlugins(loader, modConfig.Modules);
            AdminHost.Instance.ConfigureModules(services);

            //
            // CORS
            services.AddCors();

            //
            // Authentication
            services.AddBearerAuthentication();

            //
            // Authorization
            services.AddAuthorizationPolicy();

            services.AddConfigurationWriter(_hostingEnv);

            //
            // Antiforgery
            services.TryAddSingleton <IAntiforgeryTokenStore, AntiForgeryTokenStore>();
            services.AddAntiforgery(o => {
                o.RequireSsl = true;
                o.CookieName = o.FormFieldName = HeaderNames.XSRF_TOKEN;
            });

            //
            // Caching
            services.AddMemoryCache();

            //
            // MVC
            IMvcBuilder builder = services.AddMvc(o => {
                // Replace default json output formatter
                o.OutputFormatters.RemoveType <AspNetCore.Mvc.Formatters.JsonOutputFormatter>();

                var settings = JsonSerializerSettingsProvider.CreateSerializerSettings();
                o.OutputFormatters.Add(new JsonOutputFormatter(settings, System.Buffers.ArrayPool <char> .Shared));

                // TODO
                // Workaround filter to fix Object Results returned from controllers
                // Remove when https://github.com/aspnet/Mvc/issues/4960 is resolved
                o.Filters.Add(typeof(Fix4960ActionFilter));

                o.Filters.Add(typeof(ActionFoundFilter));

                o.Filters.Add(typeof(ResourceInfoFilter));

                RemoveFilter <UnsupportedContentTypeFilter>(o);
            });

            foreach (var asm in loader.GetAllLoadedAssemblies())
            {
                builder.AddApplicationPart(asm);
            }

            builder.AddControllersAsServices();
            builder.AddWebApiConventions();
        }