Ejemplo n.º 1
0
        public static void ConfigAuthentication(this IServiceCollection services)
        {
            services.Configure <AuthorizationOptions>(options =>
            {
                PermissionKeys.Configure(options);
                KnownRequirements.Configure(options);
            });

            services.AddAuthentication(DefaultAuthorizeAttribute.DefaultAuthenticationScheme)
            .AddCookie(DefaultAuthorizeAttribute.DefaultAuthenticationScheme, o =>
            {
                o.LoginPath        = new PathString("/account/login");
                o.AccessDeniedPath = new PathString("/error/forbidden");
            })
            .AddCookie(CustomerAuthorizeAttribute.CustomerAuthenticationScheme, option =>
            {
                option.LoginPath        = new PathString("/account/signin");
                option.AccessDeniedPath = new PathString("/error/forbidden");
            })
            .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidateIssuer           = true,
                    ValidIssuer              = JwtBearerDefaults.Issuer,
                    ValidateAudience         = true,
                    ValidAudience            = JwtBearerDefaults.Audience,
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(JwtBearerDefaults.IssuerSigningKey))
                };
            });
        }
Ejemplo n.º 2
0
        public static void UseZKEACMS(this IServiceCollection services, IConfiguration configuration)
        {
            //添加session
            services.AddDistributedMemoryCache();
            services.AddSession(opt =>
            {
                opt.IdleTimeout = TimeSpan.FromMinutes(20);
                //opt.Cookie.Expiration = TimeSpan.FromMinutes(30);
                //opt.Cookie.MaxAge
                opt.Cookie.HttpOnly    = true;
                opt.Cookie.IsEssential = true;
            });

            IMvcBuilder mvcBuilder = services.AddControllersWithViews(option =>
            {
                option.ModelBinderProviders.Insert(0, new WidgetTypeModelBinderProvider());
                option.ModelMetadataDetailsProviders.Add(new DataAnnotationsMetadataProvider());
            })
                                     .AddRazorOptions(opt =>
            {
                opt.ViewLocationExpanders.Clear();
                opt.ViewLocationExpanders.Add(new ThemeViewLocationExpander());
            })
                                     .AddControllersAsServices()
                                     .AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.DateFormatString = "yyyy/MM/dd H:mm";
            })
                                     .SetCompatibilityVersion(CompatibilityVersion.Latest);

            services.AddRazorPages();
            services.AddHealthChecks();
            services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            services.AddRouting(option => option.LowercaseUrls = true);

            services.TryAddScoped <IApplicationContextAccessor, ApplicationContextAccessor>();
            services.TryAddScoped <IApplicationContext, CMSApplicationContext>();
            services.TryAddSingleton <IRouteProvider, RouteProvider>();

            services.AddTransient <IRouteDataProvider, PaginationRouteDataProvider>();
            services.AddTransient <IRouteDataProvider, PostIdRouteDataProvider>();
            services.AddTransient <IRouteDataProvider, CategoryRouteDataProvider>();
            services.AddTransient <IRouteDataProvider, HtmlRouteDataProvider>();

            services.TryAddSingleton <IAdminMenuProvider, AdminMenuProvider>();
            services.TryAddScoped <IWidgetActivator, DefaultWidgetActivator>();
            services.TryAddTransient <ICarouselItemService, CarouselItemService>();
            services.TryAddTransient <ICarouselService, CarouselService>();
            services.TryAddTransient <INavigationService, NavigationService>();

            services.TryAddTransient <IDashboardProviderService, DashboardProviderService>();
            services.AddTransient <IDashboardPartDriveService, DashboardWelcomePartService>();
            services.AddTransient <IDashboardPartDriveService, DashboardEventLogPartService>();

            services.TryAddTransient <IDataArchivedService, DataArchivedService>();
            services.TryAddTransient <IExtendFieldService, ExtendFieldService>();
            services.TryAddTransient <INotifyService, NotifyService>();
            services.AddTransient <IUserCenterLinksProvider, UserCenterLinksProvider>();
            services.AddTransient <IUserCenterLinkService, UserCenterLinkService>();
            services.TryAddScoped <ILayoutService, LayoutService>();
            services.TryAddScoped <ILayoutHtmlService, LayoutHtmlService>();
            services.TryAddTransient <IMediaService, MediaService>();
            services.TryAddScoped <IPageService, PageService>();
            services.TryAddScoped <IApplicationSettingService, ApplicationSettingService>();
            services.TryAddScoped <IThemeService, ThemeService>();
            services.TryAddTransient <IWidgetTemplateService, WidgetTemplateService>();
            services.TryAddScoped <IWidgetBasePartService, WidgetBasePartService>();
            services.TryAddScoped <IZoneService, ZoneService>();
            services.TryAddTransient <Rule.IRuleService, Rule.RuleService>();

            services.AddScoped <IOnModelCreating, EntityFrameWorkModelCreating>();

            services.AddTransient <Easy.Notification.ISmtpProvider, SmtpProvider>();
            services.AddTransient <IPackageInstaller, ThemePackageInstaller>();
            services.AddTransient <IPackageInstaller, WidgetPackageInstaller>();
            services.AddTransient <IPackageInstaller, FilePackageInstaller>();
            services.AddTransient <IPackageInstaller, DataDictionaryPackageInstaller>();
            services.AddTransient <IPackageInstallerProvider, PackageInstallerProvider>();
            services.AddTransient <IEventViewerService, EventViewerService>();

            services.AddTransient <IStorage, WebStorage>();

            services.ConfigureStateProvider <StateProvider.OuterChainPictureStateProvider>();

            services.ConfigureCache <IEnumerable <WidgetBase> >();
            services.ConfigureCache <IEnumerable <ZoneEntity> >();
            services.ConfigureCache <IEnumerable <LayoutHtml> >();
            services.ConfigureCache <IEnumerable <ThemeEntity> >();
            services.ConfigureCache <List <TemplateFile> >();
            services.ConfigureCache <ConcurrentDictionary <string, object> >();
            services.ConfigureCache <string>();

            services.ConfigureMetaData <ArticleEntity, ArticleEntityMeta>();
            services.ConfigureMetaData <ArticleType, ArtycleTypeMetaData>();
            services.ConfigureMetaData <BreadcrumbWidget, BreadcrumbWidgetMetaData>();
            services.ConfigureMetaData <CarouselEntity, CarouselEntityMetaData>();
            services.ConfigureMetaData <CarouselWidget, CarouselWidgetMetaData>();
            services.ConfigureMetaData <CarouselItemEntity, CarouselItemEntityMeta>();
            services.ConfigureMetaData <HtmlWidget, HtmlWidgetMetaData>();
            services.ConfigureMetaData <ImageWidget, ImageWidgetMedaData>();
            services.ConfigureMetaData <NavigationEntity, NavigationEntityMeta>();
            services.ConfigureMetaData <NavigationWidget, NavigationWidgetMetaData>();
            services.ConfigureMetaData <ScriptWidget, ScriptWidgetMetaData>();
            services.ConfigureMetaData <StyleSheetWidget, StyleSheetWidgetMetaData>();
            services.ConfigureMetaData <VideoWidget, VideoWidgetMetaData>();
            services.ConfigureMetaData <DataArchived.DataArchived, DataArchivedMetaData>();
            services.ConfigureMetaData <ExtendFieldEntity, ExtendFieldEntityMetaData>();
            services.ConfigureMetaData <LayoutEntity, LayoutEntityMetaData>();
            services.ConfigureMetaData <MediaEntity, MediaEntityMetaData>();
            services.ConfigureMetaData <PageEntity, PageMetaData>();
            services.ConfigureMetaData <PageAsset, PageAssetMetaData>();
            services.ConfigureMetaData <ProductEntity, ProductMetaData>();
            services.ConfigureMetaData <ProductCategory, ProductCategoryMetaData>();
            services.ConfigureMetaData <ProductImage, ProductImageMetaData>();
            services.ConfigureMetaData <ApplicationSetting, ApplicationSettingMedaData>();
            services.ConfigureMetaData <ThemeEntity, ThemeEntityMetaData>();
            services.ConfigureMetaData <ZoneEntity, ZoneEntityMetaData>();
            services.ConfigureMetaData <Rule.Rule, Rule.RuleMetaData>();
            services.ConfigureMetaData <Rule.RuleItem, Rule.RuleItemMetaData>();
            services.ConfigureMetaData <SmtpSetting, SmtpSettingMetaData>();
            services.ConfigureMetaData <Robots, RobotsMetaData>();
            services.ConfigureMetaData <TemplateFile, TemplateFileMetaData>();

            services.AddScoped <IValidateService, DefaultValidateService>();

            services.AddScoped <ITemplateService, TemplateService>();

            services.Configure <NavigationWidget>(option =>
            {
                option.DataSourceLinkTitle = "导航";
                option.DataSourceLink      = "~/admin/Navigation";
            });

            services.Configure <CarouselWidget>(option =>
            {
                option.DataSourceLinkTitle = "焦点图";
                option.DataSourceLink      = "~/admin/Carousel";
            });
            #region 数据库配置
            services.AddSingleton <IDatabaseConfiguring, EntityFrameWorkConfigure>();
            services.AddSingleton <IDbConnectionPool, SimpleDbConnectionPool>();
            //池的配置:
            //MaximumRetained规定池的容量(常态最大保有数量)。
            //MaximumRetained为0时,相当于不使用DbConnection池,
            //但因为在Request期间Connection是保持打开的,所以对许多场合还是有性能改善的。
            services.AddSingleton(new DbConnectionPool.Options()
            {
                MaximumRetained = 128
            });
            //提供在Request期间租、还DbConnection的支持
            services.AddScoped <IConnectionHolder, TransientConnectionHolder>();
            services.AddDbContextOptions <CMSDbContext>();
            services.AddDbContext <CMSDbContext>();
            services.AddScoped <EasyDbContext>((provider) => provider.GetService <CMSDbContext>());
            DatabaseOption databaseOption = configuration.GetSection("Database").Get <DatabaseOption>();
            //DataTableAttribute.IsLowerCaseTableNames = databaseOption.DbType == DbTypes.MySql;
            services.AddSingleton(databaseOption);
            #endregion

            services.UseEasyFrameWork(configuration);
            foreach (IPluginStartup item in services.LoadAvailablePlugins())
            {
                item.Setup(new object[] { services, mvcBuilder });
            }

            foreach (KeyValuePair <string, Type> item in WidgetBase.KnownWidgetService)
            {
                services.TryAddTransient(item.Value);
            }
            foreach (KeyValuePair <string, Type> item in WidgetBase.KnownWidgetModel)
            {
                services.TryAddTransient(item.Value);
            }

            services.Configure <AuthorizationOptions>(options =>
            {
                PermissionKeys.Configure(options);
                KnownRequirements.Configure(options);
            });

            services.AddAuthentication(DefaultAuthorizeAttribute.DefaultAuthenticationScheme)
            .AddCookie(DefaultAuthorizeAttribute.DefaultAuthenticationScheme, o =>
            {
                o.LoginPath        = new PathString("/account/login");
                o.AccessDeniedPath = new PathString("/error/forbidden");
            })
            .AddCookie(CustomerAuthorizeAttribute.CustomerAuthenticationScheme, option =>
            {
                option.LoginPath        = new PathString("/account/signin");
                option.AccessDeniedPath = new PathString("/error/forbidden");
            });
        }
Ejemplo n.º 3
0
        public static void UseZKEACMS(this IServiceCollection services, IConfigurationRoot configuration)
        {
            services.AddMvc(option =>
            {
                option.ModelBinderProviders.Insert(0, new WidgetModelBinderProvider());
                option.ModelMetadataDetailsProviders.Add(new DataAnnotationsMetadataProvider());
            }).AddControllersAsServices().AddJsonOptions(option => { option.SerializerSettings.DateFormatString = "yyyy-MM-dd"; });

            services.TryAddScoped <IApplicationContextAccessor, ApplicationContextAccessor>();
            services.TryAddScoped <IApplicationContext, CMSApplicationContext>();
            services.TryAddSingleton <IRouteProvider, RouteProvider>();
            services.TryAddSingleton <IAdminMenuProvider, AdminMenuProvider>();
            services.TryAddTransient <IWidgetActivator, DefaultWidgetActivator>();
            services.TryAddTransient <ICarouselItemService, CarouselItemService>();
            services.TryAddTransient <ICarouselService, CarouselService>();
            services.TryAddTransient <INavigationService, NavigationService>();

            services.TryAddTransient <IDashboardProviderService, DashboardProviderService>();
            services.AddTransient <IDashboardPartDriveService, DashboardWelcomePartService>();
            services.AddTransient <IDashboardPartDriveService, DashboardEventLogPartService>();

            services.TryAddTransient <IDataArchivedService, DataArchivedService>();
            services.TryAddTransient <IExtendFieldService, ExtendFieldService>();
            services.TryAddTransient <INotifyService, NotifyService>();
            services.AddTransient <IUserCenterLinksProvider, UserCenterLinksProvider>();
            services.TryAddTransient <ILayoutService, LayoutService>();
            services.TryAddTransient <ILayoutHtmlService, LayoutHtmlService>();
            services.TryAddTransient <IMediaService, MediaService>();
            services.TryAddTransient <IPageService, PageService>();
            services.TryAddTransient <IApplicationSettingService, ApplicationSettingService>();
            services.TryAddTransient <IThemeService, ThemeService>();
            services.TryAddTransient <IWidgetTemplateService, WidgetTemplateService>();
            services.TryAddTransient <IWidgetBasePartService, WidgetBasePartService>();
            services.TryAddTransient <IZoneService, ZoneService>();
            services.AddTransient <IOnModelCreating, EntityFrameWorkModelCreating>();
            services.AddTransient <Easy.Notification.ISmtpProvider, SmtpProvider>();
            services.AddTransient <IPackageInstaller, ThemePackageInstaller>();
            services.AddTransient <IPackageInstaller, WidgetPackageInstaller>();
            services.AddTransient <IPackageInstaller, FilePackageInstaller>();
            services.AddTransient <IPackageInstaller, DataDictionaryPackageInstaller>();
            services.AddTransient <IPackageInstallerProvider, PackageInstallerProvider>();
            services.AddTransient <IEventViewerService, EventViewerService>();

            services.ConfigureMetaData <ArticleEntity, ArticleEntityMeta>();
            services.ConfigureMetaData <ArticleType, ArtycleTypeMetaData>();
            services.ConfigureMetaData <BreadcrumbWidget, BreadcrumbWidgetMetaData>();
            services.ConfigureMetaData <CarouselEntity, CarouselEntityMetaData>();
            services.ConfigureMetaData <CarouselWidget, CarouselWidgetMetaData>();
            services.ConfigureMetaData <CarouselItemEntity, CarouselItemEntityMeta>();
            services.ConfigureMetaData <HtmlWidget, HtmlWidgetMetaData>();
            services.ConfigureMetaData <ImageWidget, ImageWidgetMedaData>();
            services.ConfigureMetaData <NavigationEntity, NavigationEntityMeta>();
            services.ConfigureMetaData <NavigationWidget, NavigationWidgetMetaData>();
            services.ConfigureMetaData <ScriptWidget, ScriptWidgetMetaData>();
            services.ConfigureMetaData <StyleSheetWidget, StyleSheetWidgetMetaData>();
            services.ConfigureMetaData <VideoWidget, VideoWidgetMetaData>();
            services.ConfigureMetaData <DataArchived.DataArchived, DataArchivedMetaData>();
            services.ConfigureMetaData <ExtendFieldEntity, ExtendFieldEntityMetaData>();
            services.ConfigureMetaData <LayoutEntity, LayoutEntityMetaData>();
            services.ConfigureMetaData <MediaEntity, MediaEntityMetaData>();
            services.ConfigureMetaData <PageEntity, PageMetaData>();
            services.ConfigureMetaData <ProductEntity, ProductMetaData>();
            services.ConfigureMetaData <ProductCategory, ProductCategoryMetaData>();
            services.ConfigureMetaData <ProductImage, ProductImageMetaData>();
            services.ConfigureMetaData <ApplicationSetting, ApplicationSettingMedaData>();
            services.ConfigureMetaData <ThemeEntity, ThemeEntityMetaData>();
            services.ConfigureMetaData <ZoneEntity, ZoneEntityMetaData>();

            services.Configure <NavigationWidget>(option =>
            {
                option.DataSourceLinkTitle = "导航";
                option.DataSourceLink      = "~/admin/Navigation";
            });

            services.Configure <CarouselWidget>(option =>
            {
                option.DataSourceLinkTitle = "焦点图";
                option.DataSourceLink      = "~/admin/Carousel";
            });

            services.AddDbContext <CMSDbContext>();

            services.Configure <DatabaseOption>(configuration.GetSection("Database"));

            services.UseEasyFrameWork(configuration);
            foreach (var item in services.LoadAvailablePlugins())
            {
                item.Setup(services);
            }
            foreach (var item in WidgetBase.KnownWidgetService)
            {
                services.TryAddTransient(item.Value);
            }
            foreach (var item in WidgetBase.KnownWidgetModel)
            {
                services.TryAddTransient(item.Value);
            }

            services.Configure <AuthorizationOptions>(options =>
            {
                PermissionKeys.Configure(options);
                KnownRequirements.Configure(options);
            });

            services.AddAuthentication(DefaultAuthorizeAttribute.DefaultAuthenticationScheme)
            .AddCookie(DefaultAuthorizeAttribute.DefaultAuthenticationScheme, o =>
            {
                o.LoginPath        = new PathString("/Account/Login");
                o.AccessDeniedPath = new PathString("/Error/Forbidden");
            })
            .AddCookie(CustomerAuthorizeAttribute.CustomerAuthenticationScheme, option =>
            {
                option.LoginPath = new PathString("/Account/Signin");
            });
        }
Ejemplo n.º 4
0
        public static void UseZKEACMS(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddMvc(option =>
            {
                option.ModelBinderProviders.Insert(0, new WidgetTypeModelBinderProvider());
                option.ModelMetadataDetailsProviders.Add(new DataAnnotationsMetadataProvider());
            })
            .AddControllersAsServices()
            .AddJsonOptions(option => { option.SerializerSettings.DateFormatString = "yyyy-MM-dd"; })
            .SetCompatibilityVersion(CompatibilityVersion.Latest);

            services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            services.TryAddScoped <IApplicationContextAccessor, ApplicationContextAccessor>();
            services.TryAddScoped <IApplicationContext, CMSApplicationContext>();
            services.TryAddSingleton <IRouteProvider, RouteProvider>();

            services.AddTransient <IRouteDataProvider, PaginationRouteDataProvider>();
            services.AddTransient <IRouteDataProvider, PostIdRouteDataProvider>();
            services.AddTransient <IRouteDataProvider, CategoryRouteDataProvider>();
            services.AddTransient <IRouteDataProvider, HtmlRouteDataProvider>();

            services.TryAddSingleton <IAdminMenuProvider, AdminMenuProvider>();
            services.TryAddTransient <IWidgetActivator, DefaultWidgetActivator>();
            services.TryAddTransient <ICarouselItemService, CarouselItemService>();
            services.TryAddTransient <ICarouselService, CarouselService>();
            services.TryAddTransient <INavigationService, NavigationService>();

            services.TryAddTransient <IDashboardProviderService, DashboardProviderService>();
            services.AddTransient <IDashboardPartDriveService, DashboardWelcomePartService>();
            services.AddTransient <IDashboardPartDriveService, DashboardEventLogPartService>();

            services.TryAddTransient <IDataArchivedService, DataArchivedService>();
            services.TryAddTransient <IExtendFieldService, ExtendFieldService>();
            services.TryAddTransient <INotifyService, NotifyService>();
            services.AddTransient <IUserCenterLinksProvider, UserCenterLinksProvider>();
            services.TryAddTransient <ILayoutService, LayoutService>();
            services.TryAddTransient <ILayoutHtmlService, LayoutHtmlService>();
            services.TryAddTransient <IMediaService, MediaService>();
            services.TryAddTransient <IPageService, PageService>();
            services.TryAddTransient <IApplicationSettingService, ApplicationSettingService>();
            services.TryAddTransient <IThemeService, ThemeService>();
            services.TryAddTransient <IWidgetTemplateService, WidgetTemplateService>();
            services.TryAddTransient <IWidgetBasePartService, WidgetBasePartService>();
            services.TryAddTransient <IZoneService, ZoneService>();
            services.TryAddTransient <Rule.IRuleService, Rule.RuleService>();

            services.AddScoped <IOnModelCreating, EntityFrameWorkModelCreating>();

            services.AddTransient <Easy.Notification.ISmtpProvider, SmtpProvider>();
            services.AddTransient <IPackageInstaller, ThemePackageInstaller>();
            services.AddTransient <IPackageInstaller, WidgetPackageInstaller>();
            services.AddTransient <IPackageInstaller, FilePackageInstaller>();
            services.AddTransient <IPackageInstaller, DataDictionaryPackageInstaller>();
            services.AddTransient <IPackageInstallerProvider, PackageInstallerProvider>();
            services.AddTransient <IEventViewerService, EventViewerService>();

            services.ConfigureCache <IEnumerable <WidgetBase> >();
            services.ConfigureCache <IEnumerable <ZoneEntity> >();
            services.ConfigureCache <IEnumerable <LayoutHtml> >();

            services.ConfigureMetaData <ArticleEntity, ArticleEntityMeta>();
            services.ConfigureMetaData <ArticleType, ArtycleTypeMetaData>();
            services.ConfigureMetaData <BreadcrumbWidget, BreadcrumbWidgetMetaData>();
            services.ConfigureMetaData <CarouselEntity, CarouselEntityMetaData>();
            services.ConfigureMetaData <CarouselWidget, CarouselWidgetMetaData>();
            services.ConfigureMetaData <CarouselItemEntity, CarouselItemEntityMeta>();
            services.ConfigureMetaData <HtmlWidget, HtmlWidgetMetaData>();
            services.ConfigureMetaData <ImageWidget, ImageWidgetMedaData>();
            services.ConfigureMetaData <NavigationEntity, NavigationEntityMeta>();
            services.ConfigureMetaData <NavigationWidget, NavigationWidgetMetaData>();
            services.ConfigureMetaData <ScriptWidget, ScriptWidgetMetaData>();
            services.ConfigureMetaData <StyleSheetWidget, StyleSheetWidgetMetaData>();
            services.ConfigureMetaData <VideoWidget, VideoWidgetMetaData>();
            services.ConfigureMetaData <DataArchived.DataArchived, DataArchivedMetaData>();
            services.ConfigureMetaData <ExtendFieldEntity, ExtendFieldEntityMetaData>();
            services.ConfigureMetaData <LayoutEntity, LayoutEntityMetaData>();
            services.ConfigureMetaData <MediaEntity, MediaEntityMetaData>();
            services.ConfigureMetaData <PageEntity, PageMetaData>();
            services.ConfigureMetaData <ProductEntity, ProductMetaData>();
            services.ConfigureMetaData <ProductCategory, ProductCategoryMetaData>();
            services.ConfigureMetaData <ProductImage, ProductImageMetaData>();
            services.ConfigureMetaData <ApplicationSetting, ApplicationSettingMedaData>();
            services.ConfigureMetaData <ThemeEntity, ThemeEntityMetaData>();
            services.ConfigureMetaData <ZoneEntity, ZoneEntityMetaData>();
            services.ConfigureMetaData <Rule.Rule, Rule.RuleMetaData>();
            services.ConfigureMetaData <Rule.RuleItem, Rule.RuleItemMetaData>();
            services.ConfigureMetaData <SmtpSetting, SmtpSettingMetaData>();
            services.ConfigureMetaData <Robots, RobotsMetaData>();

            services.Configure <NavigationWidget>(option =>
            {
                option.DataSourceLinkTitle = "导航";
                option.DataSourceLink      = "~/admin/Navigation";
            });

            services.Configure <CarouselWidget>(option =>
            {
                option.DataSourceLinkTitle = "焦点图";
                option.DataSourceLink      = "~/admin/Carousel";
            });
            #region 数据库配置
            services.AddSingleton <SimpleDbConnectionPool>();
            //池的配置:
            //MaximumRetained规定池的容量(常态最大保有数量)。
            //MaximumRetained为0时,相当于不使用DbConnection池,
            //但因为在Request期间Connection是保持打开的,所以对许多场合还是有性能改善的。
            services.AddSingleton(new SimpleDbConnectionPool.Options()
            {
                MaximumRetained = 128
            });
            //提供在Request期间租、还DbConnection的支持
            services.AddScoped <SimpleDbConnectionPool.TransientObjectHolder>();
            services.AddScoped <DbContextOptions <CMSDbContext> >(sp =>
            {
                //租一个DbConnection(将在Request完成后还回,因为其Lifetime为Scoped类型)
                SimpleDbConnectionPool.TransientObjectHolder holder   = sp.GetService <SimpleDbConnectionPool.TransientObjectHolder>();
                SimpleDbConnectionPool.IDatabaseConfiguring configure = sp.GetService <SimpleDbConnectionPool.IDatabaseConfiguring>();
                DbContextOptionsBuilder <CMSDbContext> optBuilder     = new DbContextOptionsBuilder <CMSDbContext>();
                configure.OnConfiguring(optBuilder, holder.Object);
                return(optBuilder.Options);
            });
            services.AddDbContext <CMSDbContext>(ServiceLifetime.Scoped);
            services.AddScoped <EasyDbContext>((provider) => provider.GetService <CMSDbContext>());
            services.AddSingleton(configuration.GetSection("Database").Get <DatabaseOption>());
            #endregion

            services.UseEasyFrameWork(configuration);
            foreach (IPluginStartup item in services.LoadAvailablePlugins())
            {
                item.Setup(services);
            }
            foreach (KeyValuePair <string, Type> item in WidgetBase.KnownWidgetService)
            {
                services.TryAddTransient(item.Value);
            }
            foreach (KeyValuePair <string, Type> item in WidgetBase.KnownWidgetModel)
            {
                services.TryAddTransient(item.Value);
            }

            services.Configure <AuthorizationOptions>(options =>
            {
                PermissionKeys.Configure(options);
                KnownRequirements.Configure(options);
            });

            services.AddAuthentication(DefaultAuthorizeAttribute.DefaultAuthenticationScheme)
            .AddCookie(DefaultAuthorizeAttribute.DefaultAuthenticationScheme, o =>
            {
                o.LoginPath        = new PathString("/Account/Login");
                o.AccessDeniedPath = new PathString("/Error/Forbidden");
            })
            .AddCookie(CustomerAuthorizeAttribute.CustomerAuthenticationScheme, option =>
            {
                option.LoginPath = new PathString("/Account/Signin");
            });
        }
Ejemplo n.º 5
0
        public static void UseZKEACMS(this IServiceCollection services, IConfiguration configuration)
        {
            //添加session
            services.AddDistributedMemoryCache();
            services.AddSession(opt =>
            {
                opt.IdleTimeout = TimeSpan.FromMinutes(20);
                //opt.Cookie.Expiration = TimeSpan.FromMinutes(30);
                //opt.Cookie.MaxAge
                opt.Cookie.HttpOnly    = true;
                opt.Cookie.IsEssential = true;
            });

            IMvcBuilder mvcBuilder = services.AddControllersWithViews(option =>
            {
                option.ModelBinderProviders.Insert(0, new WidgetTypeModelBinderProvider());
                option.ModelMetadataDetailsProviders.Add(new DataAnnotationsMetadataProvider());
            })
                                     .AddRazorOptions(opt =>
            {
                opt.ViewLocationExpanders.Clear();
                opt.ViewLocationExpanders.Add(new ThemeViewLocationExpander());
            })
                                     .AddControllersAsServices()
                                     .AddDataAnnotationsLocalization(option => option.DataAnnotationLocalizerProvider = (t, factory) =>
            {
                if (t.IsClass)
                {
                    return(new LocalizeString(t));
                }
                else
                {
                    return(null);
                }
            })
                                     .SetCompatibilityVersion(CompatibilityVersion.Latest);

            services.AddRazorPages();
            services.AddHealthChecks();
            services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            services.AddRouting(option => option.LowercaseUrls = true);

            services.TryAddScoped <IApplicationContextAccessor, ApplicationContextAccessor>();
            services.TryAddScoped <IApplicationContext, CMSApplicationContext>();
            services.TryAddSingleton <IRouteProvider, RouteProvider>();

            services.AddTransient <IRouteDataProvider, PaginationRouteDataProvider>();
            services.AddTransient <IRouteDataProvider, PostIdRouteDataProvider>();
            services.AddTransient <IRouteDataProvider, CategoryRouteDataProvider>();
            services.AddTransient <IRouteDataProvider, HtmlRouteDataProvider>();

            services.TryAddSingleton <IAdminMenuProvider, AdminMenuProvider>();
            services.TryAddScoped <IWidgetActivator, DefaultWidgetActivator>();
            services.TryAddTransient <ICarouselItemService, CarouselItemService>();
            services.TryAddTransient <ICarouselService, CarouselService>();
            services.TryAddTransient <INavigationService, NavigationService>();
            services.TryAddTransient <Site.ISiteInformationService, Site.SiteInformationService>();

            services.TryAddTransient <IDashboardProviderService, DashboardProviderService>();
            services.AddTransient <IDashboardPartDriveService, DashboardWelcomePartService>();
            services.AddTransient <IDashboardPartDriveService, DashboardEventLogPartService>();

            services.TryAddTransient <IDataArchivedService, DataArchivedService>();
            services.TryAddTransient <IExtendFieldService, ExtendFieldService>();
            services.TryAddTransient <INotifyService, NotifyService>();
            services.AddTransient <IUserCenterLinksProvider, UserCenterLinksProvider>();
            services.AddTransient <IUserCenterLinkService, UserCenterLinkService>();
            services.TryAddScoped <ILayoutService, LayoutService>();
            services.TryAddScoped <ILayoutHtmlService, LayoutHtmlService>();
            services.TryAddTransient <IMediaService, MediaService>();
            services.TryAddScoped <IPageService, PageService>();
            services.TryAddScoped <IApplicationSettingService, ApplicationSettingService>();
            services.TryAddScoped <IThemeService, ThemeService>();
            services.TryAddTransient <IWidgetTemplateService, WidgetTemplateService>();
            services.TryAddScoped <IWidgetBasePartService, WidgetBasePartService>();
            services.TryAddScoped <IZoneService, ZoneService>();
            services.TryAddScoped <IHostOptionProvider, HostOptionProvider>();
            services.TryAddTransient <Rule.IRuleService, Rule.RuleService>();

            services.AddScoped <IOnModelCreating, EntityFrameWorkModelCreating>();

            services.AddTransient <Easy.Notification.ISmtpProvider, SmtpProvider>();
            services.AddTransient <IPackageInstaller, ThemePackageInstaller>();
            services.AddTransient <IPackageInstaller, WidgetPackageInstaller>();
            services.AddTransient <IPackageInstaller, FilePackageInstaller>();
            services.AddTransient <IPackageInstaller, DataDictionaryPackageInstaller>();
            services.AddTransient <IPackageInstallerProvider, PackageInstallerProvider>();
            services.AddTransient <IEventViewerService, EventViewerService>();
            services.AddTransient <IDBContextProvider, DefaultDBContextProvider>();
            services.AddScoped <IEventManager, EventManager>();

            services.AddTransient <IStorage, WebStorage>();
            services.AddTransient <ICurrencyService, CurrencyService>();

            services.ConfigureStateProvider <StateProvider.OuterChainPictureStateProvider>();
            services.ConfigureStateProvider <StateProvider.EnableResponsiveDesignStateProvider>();
            services.ConfigureStateProvider <Site.SiteInformationStateProvider>();
            services.ConfigureStateProvider <CurrencyStateProvider>();

            services.ConfigureCache <IEnumerable <WidgetBase> >();
            services.ConfigureCache <IEnumerable <ZoneEntity> >();
            services.ConfigureCache <IEnumerable <LayoutHtml> >();
            services.ConfigureCache <IEnumerable <ThemeEntity> >();
            services.ConfigureCache <List <TemplateFile> >();
            services.ConfigureCache <ConcurrentDictionary <string, object> >();
            services.ConfigureCache <string>();

            services.ConfigureMetaData <AdminSignViewModel, AdminSignViewModelMetaData>();
            services.ConfigureMetaData <SignInViewModel, SignInViewModelMetaData>();
            services.ConfigureMetaData <CustomerSignInViewModel, CustomerSignInViewModelMetaData>();
            services.ConfigureMetaData <ArticleEntity, ArticleEntityMeta>();
            services.ConfigureMetaData <ArticleGallery, ArticleGalleryMetaData>();
            services.ConfigureMetaData <ArticleGalleryItem, ArticleGalleryItemMetaData>();
            services.ConfigureMetaData <ArticleItem, ArticleGalleryProductMetaData>();
            services.ConfigureMetaData <ArticleType, ArtycleTypeMetaData>();
            services.ConfigureMetaData <BreadcrumbWidget, BreadcrumbWidgetMetaData>();
            services.ConfigureMetaData <CarouselEntity, CarouselEntityMetaData>();
            services.ConfigureMetaData <CarouselWidget, CarouselWidgetMetaData>();
            services.ConfigureMetaData <CarouselItemEntity, CarouselItemEntityMeta>();
            services.ConfigureMetaData <HtmlWidget, HtmlWidgetMetaData>();
            services.ConfigureMetaData <ImageWidget, ImageWidgetMedaData>();
            services.ConfigureMetaData <NavigationEntity, NavigationEntityMeta>();
            services.ConfigureMetaData <NavigationWidget, NavigationWidgetMetaData>();
            services.ConfigureMetaData <ScriptWidget, ScriptWidgetMetaData>();
            services.ConfigureMetaData <StyleSheetWidget, StyleSheetWidgetMetaData>();
            services.ConfigureMetaData <VideoWidget, VideoWidgetMetaData>();
            services.ConfigureMetaData <DataArchived.DataArchived, DataArchivedMetaData>();
            services.ConfigureMetaData <ExtendFieldEntity, ExtendFieldEntityMetaData>();
            services.ConfigureMetaData <LayoutEntity, LayoutEntityMetaData>();
            services.ConfigureMetaData <MediaEntity, MediaEntityMetaData>();
            services.ConfigureMetaData <PageEntity, PageMetaData>();
            services.ConfigureMetaData <PageAsset, PageAssetMetaData>();
            services.ConfigureMetaData <ProductEntity, ProductMetaData>();
            services.ConfigureMetaData <ProductCategory, ProductCategoryMetaData>();
            services.ConfigureMetaData <ProductImage, ProductImageMetaData>();
            services.ConfigureMetaData <ProductGallery, ProductGalleryMetaData>();
            services.ConfigureMetaData <ProductGalleryItem, ProductGalleryItemMetaData>();
            services.ConfigureMetaData <ProductItem, ProductGalleryProductMetaData>();
            services.ConfigureMetaData <ApplicationSetting, ApplicationSettingMedaData>();
            services.ConfigureMetaData <ThemeEntity, ThemeEntityMetaData>();
            services.ConfigureMetaData <ZoneEntity, ZoneEntityMetaData>();
            services.ConfigureMetaData <Rule.Rule, Rule.RuleMetaData>();
            services.ConfigureMetaData <Rule.RuleItem, Rule.RuleItemMetaData>();
            services.ConfigureMetaData <Robots, RobotsMetaData>();
            services.ConfigureMetaData <TemplateFile, TemplateFileMetaData>();
            services.ConfigureMetaData <TabWidget, TabWidgetMetaData>();
            services.ConfigureMetaData <TabItem, TabItemMetaData>();
            services.ConfigureMetaData <HeadWidget, HeaderWidgetMetaData>();
            services.ConfigureMetaData <ForgottenViewModel, ForgottenViewModelMetaData>();
            services.ConfigureMetaData <ResetViewModel, ResetViewModelMetaData>();

            services.ConfigureMetaData <CurrencyEntry, CurrencyEntryMetaData>();
            services.ConfigureMetaData <CurrencyOption, CurrencyOptionMetaData>();

            services.RegistEvent <RemoveCacheOnPagePublishedEventHandler>(Events.OnPagePublished);
            services.RegistEvent <RemoveOldVersionOnPagePublishedEventHandler>(Events.OnPagePublished);
            services.RegistEvent <RemoveCacheOnPageDeletedEventHandler>(Events.OnPageDeleted);
            services.RegistEvent <WidgetChangedTriggerPageEventHandler>(Events.OnWidgetAdded, Events.OnWidgetUpdated, Events.OnWidgetDeleted, Events.OnWidgetBasePartUpdated);

            services.AddScoped <IImageCaptchaService, ImageCaptchaService>();
            services.AddScoped <IImageGenerator, DefaultImageGenerator>();
            services.AddScoped <ICaptchaCodeStorageProvider, SessionCaptchaCodeStorageProvider>();

            services.AddScoped <ITemplateService, TemplateService>();

            services.Configure <NavigationWidget>(option =>
            {
                option.DataSourceLinkTitle = "Navigation";
                option.DataSourceLink      = "~/admin/Navigation";
            });

            services.Configure <CarouselWidget>(option =>
            {
                option.DataSourceLinkTitle = "Carousel";
                option.DataSourceLink      = "~/admin/Carousel";
            });
            #region 数据库配置
            services.AddSingleton <IDatabaseConfiguring, EntityFrameWorkConfigure>();
            services.AddSingleton <IDbConnectionPool, SimpleDbConnectionPool>();
            //池的配置:
            //MaximumRetained规定池的容量(常态最大保有数量)。
            //MaximumRetained为0时,相当于不使用DbConnection池,
            //但因为在Request期间Connection是保持打开的,所以对许多场合还是有性能改善的。
            services.AddSingleton(new DbConnectionPool.Options()
            {
                MaximumRetained = 128
            });
            //提供在Request期间租、还DbConnection的支持
            services.AddScoped <IConnectionHolder, TransientConnectionHolder>();
            services.AddDbContextOptions <CMSDbContext>();
            services.AddDbContext <CMSDbContext>();
            services.AddScoped <EasyDbContext>((provider) => provider.GetService <CMSDbContext>());
            DatabaseOption databaseOption = configuration.GetSection("Database").Get <DatabaseOption>();
            DataTableAttribute.IsLowerCaseTableNames = databaseOption.IsLowerCaseTableNames;
            services.AddSingleton(databaseOption);
            #endregion
            services.Configure <HostOption>(configuration.GetSection("Host"));

            services.UseEasyFrameWork(configuration);
            foreach (IPluginStartup item in services.LoadAvailablePlugins())
            {
                item.Setup(new object[] { services, mvcBuilder });
            }

            foreach (KeyValuePair <string, Type> item in WidgetBase.KnownWidgetService)
            {
                services.TryAddTransient(item.Value);
            }
            foreach (KeyValuePair <string, Type> item in WidgetBase.KnownWidgetModel)
            {
                services.TryAddTransient(item.Value);
            }

            services.Configure <AuthorizationOptions>(options =>
            {
                PermissionKeys.Configure(options);
                KnownRequirements.Configure(options);
            });

            services.AddAuthentication(DefaultAuthorizeAttribute.DefaultAuthenticationScheme)
            .AddCookie(DefaultAuthorizeAttribute.DefaultAuthenticationScheme, o =>
            {
                o.LoginPath        = new PathString("/account/login");
                o.AccessDeniedPath = new PathString("/error/forbidden");
            })
            .AddCookie(CustomerAuthorizeAttribute.CustomerAuthenticationScheme, option =>
            {
                option.LoginPath        = new PathString("/account/signin");
                option.AccessDeniedPath = new PathString("/error/forbidden");
            })
            .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidateIssuer           = true,
                    ValidIssuer              = JwtBearerDefaults.Issuer,
                    ValidateAudience         = true,
                    ValidAudience            = JwtBearerDefaults.Audience,
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(JwtBearerDefaults.IssuerSigningKey))
                };
            });
        }