// This method gets called by the runtime. Use this method to add services to the container. public static void ConfigureServices(this IServiceCollection services) { APP.ConfigureSiteAllModulesServices(services); services.AddMediatR(typeof(WebSetup).Assembly); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddSingleton <ICompiler, Compiler>() .AddSingleton <DynamicActionProvider>() .AddSingleton <DynamicChangeTokenProvider>() .AddSingleton <IActionDescriptorProvider>(provider => provider.GetRequiredService <DynamicActionProvider>()) .AddSingleton <IActionDescriptorChangeProvider>(provider => provider.GetRequiredService <DynamicChangeTokenProvider>()); services.Configure <CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); services.AddSession(o => { o.IdleTimeout = TimeSpan.FromSeconds(60 * 60); o.Cookie.HttpOnly = true; }); // 配置授权 var appConfig = APPCommon.AppConfig; var signingkey = appConfig.GetDicstring("JWT.IssuerSigningKey"); var backendfoldername = appConfig.GetDicstring("backend.foldername"); var AuthorizeCookiesName = appConfig.GetDicstring("AuthorizeCookiesName"); var OdicCookiesName = appConfig.GetDicstring("OdicCookiesName"); var identityserver4url = appConfig.GetDicstring("Identityserver4.url"); var Identityserver4ApiResouceKey = appConfig.GetDicstring("Identityserver4.ApiResouceKey"); var idsr4_ClientId = appConfig.GetDicstring("idsr4_ClientId"); var idsr4_ClientSecret = appConfig.GetDicstring("idsr4_ClientSecret"); var idsr4_ReponseType = appConfig.GetDicstring("idsr4_ReponseType"); var OauthClientConfig_scopes = appConfig.GetDicstring("OauthClientConfig_scopes"); var IsUsingIdentityserverClient = Convert.ToBoolean(appConfig.GetDicstring("IsUsingIdentityserverClient")); var IsUsingIdentityserver4 = Convert.ToBoolean(appConfig.GetDicstring("IsUsingIdentityserver4")); var isAllowCros = Convert.ToBoolean(appConfig.GetDicstring("isAllowCros")); var AllowCrosUrl = appConfig.GetDicstring("AllowCrosUrl"); var IsUserAutoUpdatedb = Convert.ToBoolean(appConfig.GetDicstring("IsUserAutoUpdatedb")); var isEnableSwagger = Convert.ToBoolean(appConfig.GetDicstring("isEnableSwagger"));; //services.AddMvcCore().AddAuthorization().AddJsonFormatters(); var IsUsingLocalIndentity = Convert.ToBoolean(appConfig.GetDicstring("IsUsingLocalIndentity")); // var IsUsingDB = Convert.ToBoolean(appConfig.GetDicstring("IsUsingDB")); var IsUsingCmsGlobalRouterFilter = Convert.ToBoolean(appConfig.GetDicstring("IsUsingCmsGlobalRouterFilter")); if (IsUserAutoUpdatedb) { InitDbMigrate(); } APP.InitDbMigrate(); APPCommon.AppConfig.LoadDBDictionary(); APPCommon.LoadLocalLangs(); if (isEnableSwagger) { //注册Swagger生成器,定义一个和多个Swagger 文档 services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo() { Version = "v1", Title = " API", Description = "A simple example ASP.NET Core Web API", Contact = new OpenApiContact { Name = "Oliver Wa", Email = string.Empty, Url = new Uri("http://www.66wave.com/") }, License = new OpenApiLicense { Name = "许可证名字", Url = new Uri("http://www.66wave.com/") } }); }); } //services.AddAuthorization(options => //{ // options.AddPolicy("Adult1", policy => // { // }); // options.AddPolicy("Adult2", policy => // { // }); //}); SetupIdentity(services, appConfig, signingkey, backendfoldername, AuthorizeCookiesName, OdicCookiesName, identityserver4url, Identityserver4ApiResouceKey, idsr4_ClientId, idsr4_ClientSecret, OauthClientConfig_scopes, IsUsingIdentityserverClient, IsUsingIdentityserver4, IsUsingLocalIndentity, idsr4_ReponseType); if (isAllowCros) { services.AddCors(options => { options.AddPolicy("allow_all", bb => { if (AllowCrosUrl == "*") { bb = bb.AllowAnyOrigin(); } else { var allusrs = AllowCrosUrl.Split(','); bb = bb.WithOrigins(allusrs); } bb.AllowAnyOrigin(); }); }); } // _ = services.AddMvc(o => _是什么 IMvcBuiler services.AddRazorPages(c => { }); services.Configure <RazorViewEngineOptions>(options => { options.AreaViewLocationFormats.Add("~/Areas/" + appConfig.AdminAreas + "/{ 2}/Views/{1}/{0}.cshtml"); options.AreaViewLocationFormats.Add("~/Areas/" + appConfig.AdminAreas + "/{2}/Views/Shared/{0}.cshtml"); options.ViewLocationExpanders.Add(new TemplateViewLocationExpander(Configuration)); }); services.AddControllersWithViews(o => { o.Filters.Add <WorkAuthorzation>(); // 添加身份验证过滤器 o.Filters.Add <ControllerActionFilter>(); if (IsUsingCmsGlobalRouterFilter) { o.Filters.Add <GlobalActionFilter>(); //o.Filters.Add<WorkAuthorzation>(); } }) .AddRazorRuntimeCompilation(options => { //var libraryPath = Path.GetFullPath( // Path.Combine(AppRootDir , "UniOrm.Startup.Web")); options.FileProviders.Add(new PhysicalFileProvider(APPCommon.AppRootDir)); }) .AddNewtonsoftJson(options => { options.SerializerSettings.ContractResolver = new DefaultContractResolver(); CustomizedDateTimeConverer converer = new CustomizedDateTimeConverer(); //然后需要使用到的dto对象或者实体对象,打上这个特性即可,如下所示: [JsonConverter(typeof(CustomizedDateTimeConverer), “yyyy - MM - dd HH: mm:ss”)] options.SerializerSettings.Converters.Add(converer); }) .AddRazorPagesOptions( options => { var razorDir = "/" + appConfig.UserSpaceDir + "/RazoPages"; razorDir.ToServerFullPathEnEnsure(); options.RootDirectory = razorDir; options.Conventions.AuthorizePage("/test1", "OnlyAdminAccess"); options.Conventions.AuthorizeFolder("/Admin/", "OnlyAdminAccess"); //url重写 //options.Conventions.AddPageRoute("/Post", "Post/{year}/{month}/{day}"); //以下示例将 URL www.domain.com/product 映射到Razor 页面 “extras”文件夹“products.cshtml”文件: //options.Conventions.AddPageRoute("/extras/products", "product"); //最后一个例子说明将所有请求映射到单个文件。如果站点内容存储在特定位置(数据库,Markdown文件),并且由单个文件(例如 “index.cshtml” ) //负责根据 URL 定位内容,然后将其处理为HTML,则可以执行此操作: //options.Conventions.AddPageRoute("/index", "{*url}"); //默认关闭 防止跨站请求伪造(CSRF / XSRF)攻击 options.Conventions.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute()); }) .ConfigureApplicationPartManager(m => { //多个项目中分离Asp.Net Core Mvc的Controller和Areas //var homeType = typeof(Web.Controllers.Areas.HomeController); //var controllerAssembly = homeType.GetTypeInfo().Assembly; //var feature = new ControllerFeature(); //m.ApplicationParts.Add(new AssemblyPart(controllerAssembly)); //m.PopulateFeature(feature); //services.AddSingleton(feature.Controllers.Select(t => t.AsType()).ToArray()); }) .InitializeTagHelper <FormTagHelper>((helper, context) => helper.Antiforgery = false); var asses = AppDomain.CurrentDomain.GetAssemblies(); // services.AddControllers().AddControllersAsServices(); //控制器当做实例创建 // services.InitAutofac(asses); // APPCommon.Builder.RegisterType<IHttpContextAccessor, HttpContextAccessor>(); appConfig.ResultDictionary = appConfig.ResultDictionary; APP.ApplicationServices = services.BuildServiceProvider(); APP.SetServiceProvider(); }