Ejemplo n.º 1
0
Archivo: Startup.cs Proyecto: ntzw/cms
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.TryAddSingleton <IActionResultExecutor <ResultBase>, ResultBaseExecutor>();
            services.AddMemoryCache();
            services.AddDistributedMemoryCache();
            services.AddSession();
            services.AddSingleton(HtmlEncoder.Create(UnicodeRanges.All));
            services.AddAuthentication().AddCookie(AdminCookieAttribute.Scheme, o =>
            {
                o.ClaimsIssuer        = AdminCookieAttribute.ClaimsIssuer;
                o.LoginPath           = new PathString("/Admin/Login");
                o.Cookie.SameSite     = SameSiteMode.Unspecified;
                o.Cookie.SecurePolicy = CookieSecurePolicy.None;

                o.Events = new CookieAuthenticationEvents
                {
                    OnValidatePrincipal = async context =>
                    {
                        var admin = AdminCookieAttribute.GetLoginAdmin(context.Principal.Claims);
                        if (admin == null || admin.Id.ToInt() <= 0)
                        {
                            await AdminValidateFail(context);
                            return;
                        }

                        AdminCookieAttribute.SetLoginAdmin(admin);
                    }
                };
            });

            services.AddControllersWithViews(options =>
            {
                options.InputFormatters.Insert(0, new JsonInputFormatter());
            });

            services.AddRazorPages().AddRazorRuntimeCompilation();

            var baseType             = typeof(IModuleInitializer);
            var path                 = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
            var referencedAssemblies = System.IO.Directory.GetFiles(path, "*.dll").ToList().FindAll(temp =>
            {
                string fileName = Path.GetFileName(temp);
                return(fileName.StartsWith("CMS.Modules"));
            }).Select(Assembly.LoadFrom).ToArray();
            var types = referencedAssemblies
                        .SelectMany(a => a.DefinedTypes)
                        .Select(type => type.AsType())
                        .Where(x => x != baseType && baseType.IsAssignableFrom(x)).ToArray();
            var implementTypes = types.Where(x => x.IsClass).ToArray();

            foreach (var implementType in implementTypes)
            {
                services.AddSingleton(baseType, implementType);
            }

            var sp = services.BuildServiceProvider();
            var moduleInitializers = sp.GetServices <IModuleInitializer>();

            foreach (var moduleInitializer in moduleInitializers)
            {
                moduleInitializer.ConfigureServices(services);
            }
        }