/// <summary>
        /// Adds services required for application session state.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
        /// <param name="configure">The session options to configure the middleware with.</param>
        /// <returns>The <see cref="IServiceCollection"/>.</returns>
        public static IServiceCollection AddSession(this IServiceCollection services, Action<SessionOptions> configure)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            services.Configure(configure);
            return services.AddSession();
        }
예제 #2
0
        public static IServiceCollection AddUmbraco(this IServiceCollection services)
        {
            services.AddUmbracoCore();
            services.AddCaching();
            services.AddSession();
            services.AddMvc();
            //services.AddAuthentication();
            //services.AddAuthorization();

            //replace the MVC one
            services.AddSingleton<IAssemblyProvider, MvcPluginAssemblyProvider>();

            services.AddSingleton<IUmbracoAssemblyProvider, PluginAssemblyProvider>(provider =>
            {
                var hosting = provider.GetRequiredService<IApplicationEnvironment>();
                var fileProvider = new PhysicalFileProvider(hosting.ApplicationBasePath);
                return new PluginAssemblyProvider(fileProvider, provider.GetRequiredService<ILoggerFactory>(),
                    PlatformServices.Default.AssemblyLoadContextAccessor,
                    PlatformServices.Default.AssemblyLoaderContainer);
            });

            services.AddAuthorization(options =>
            {
                options.AddPolicy(
                    "umbraco-backoffice",
                    builder => builder
                        .AddAuthenticationSchemes("umbraco-backoffice")
                        .RequireAuthenticatedUser()
                        .RequireClaim("umbraco-backoffice")
                    );
            });

            services.Configure<MvcOptions>(options =>
            {
                options.ModelBinders.Insert(0, new PublishedContentModelBinder());
            });

            //services.AddIdentity<BackOfficeUser, IdentityRole>();

            services.AddSingleton<IControllerActivator, UmbracoControllerActivator>();
            services.AddTransient<UmbracoControllerHelper>();
            //services.AddSingleton<UmbracoAssemblyProvider>();
            services.AddSingleton<IUmbracoConfig, UmbracoConfig>();
            services.AddSingleton<UmbracoControllerTypeCollection>();
            services.AddSingleton<SurfaceFormHelper>();
            services.AddSingleton<IControllerPropertyActivator, ProxiedViewDataDictionaryPropertyActivator>();

            services.AddScoped<UmbracoContext>();
            services.AddScoped<RoutingContext>();
            services.AddScoped<PublishedContentRequest>();

            //TODO: default is no last chance finder (for now)
            services.AddScoped<ILastChanceContentFinder>(provider => (ILastChanceContentFinder) null);
            services.AddScoped<UrlProvider>(provider => new UrlProvider(
                provider.GetRequiredService<UmbracoContext>(),
                provider.GetServices<IUrlProvider>(),
                UrlProviderMode.Auto));

            return services;
        }
        /// <summary>
        /// Configures custom services to add to the ASP.NET MVC 6 Injection of Control (IoC) container.
        /// </summary>
        /// <param name="services">The services collection or IoC container.</param>
        public static void ConfigureCoreServices(this IServiceCollection services)
        {
            #region Antiforgery
            services.ConfigureAntiforgery(
                    antiforgeryOptions =>
                    {
                        // Rename the Anti-Forgery cookie from "__RequestVerificationToken" to "f". This adds a little 
                        // security through obscurity and also saves sending a few characters over the wire. 
                        antiforgeryOptions.CookieName = "f";

                        // Rename the form input name from "__RequestVerificationToken" to "f" for the same reason above 
                        // e.g. <input name="__RequestVerificationToken" type="hidden" value="..." />
                        antiforgeryOptions.FormFieldName = "f";

                        // If you have enabled SSL/TLS. Uncomment this line to ensure that the Anti-Forgery cookie requires 
                        // SSL /TLS to be sent across the wire. 
                        // $Start-HttpsEverywhere$
                        // antiforgeryOptions.RequireSsl = true;
                        // $End-HttpsEverywhere$
                    });
            services.AddAntiforgery();
            #endregion

            #region Configuration

            var applicationEnvironment = services.BuildServiceProvider().GetRequiredService<IApplicationEnvironment>();
            var hostingEnvironment = services.BuildServiceProvider().GetRequiredService<IHostingEnvironment>();
            //var configurationPath = Path.Combine(applicationEnvironment.ApplicationBasePath, "config.json");

            // Set up configuration sources.
            var configBuilder = new ConfigurationBuilder()
                .SetBasePath(applicationEnvironment.ApplicationBasePath)
                .AddJsonFile("appsettings.json")
                .AddJsonFile($"appsettings.{hostingEnvironment.EnvironmentName}.json", optional: true)
                //All environment variables in the process's context flow in as configuration values.
                .AddEnvironmentVariables();

            Configuration = configBuilder.Build();

            services.AddSingleton(_ => Configuration as IConfiguration);

           
            #endregion  

            #region Logging


            services.AddLogging();
            

            #endregion

           

            #region Caching
            // Adds a default in-memory implementation of IDistributedCache, which is very fast but 
            // the cache will not be shared between instances of the application. 
            // Also adds IMemoryCache.
            services.AddCaching();

            // Uncomment the following line to use the Redis implementation of      
            // IDistributedCache. This will override any previously registered IDistributedCache 
            // service. Redis is a very fast cache provider and the recommended distributed cache 
            // provider.
            // services.AddTransient<IDistributedCache, RedisCache>();

            // Uncomment the following line to use the Microsoft SQL Server implementation of 
            // IDistributedCache. Note that this would require setting up the session state database.
            // Redis is the preferred cache implementation but you can use SQL Server if you don't 
            // have an alternative.
            // services.AddSqlServerCache(o =>
            // {
            //     o.ConnectionString = 
            //       "Server=.;Database=ASPNET5SessionState;Trusted_Connection=True;";
            //     o.SchemaName = "dbo";
            //     o.TableName = "Sessions";
            // });
            #endregion

            #region Session
            services.AddSession(o =>
            {
                //o.IdleTimeout = TimeSpan.FromMinutes(Double.Parse(Configuration[TimeoutConfigKey]));
                o.IdleTimeout = TimeSpan.FromMinutes(15);
            });
            #endregion

            #region Localization
            services.AddLocalization();
            #endregion

        }