// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { string pathToCryptoKeys = Path.Combine(environment.ContentRootPath, "dp_keys"); services.AddDataProtection() .PersistKeysToFileSystem(new System.IO.DirectoryInfo(pathToCryptoKeys)); services.Configure <ForwardedHeadersOptions>(options => { options.ForwardedHeaders = Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedProto; }); services.AddMemoryCache(); //services.AddSession(); ConfigureAuthPolicy(services); services.AddOptions(); var connectionString = Configuration.GetConnectionString("EntityFrameworkConnection"); services.AddCloudscribeCoreEFStorageMSSQL(connectionString); // only needed if using cloudscribe logging with EF storage services.AddCloudscribeLoggingEFStorageMSSQL(connectionString); services.AddCloudscribeSimpleContentEFStorageMSSQL(connectionString); services.AddCloudscribeLogging(); services.AddScoped <cloudscribe.Web.Navigation.INavigationNodePermissionResolver, cloudscribe.Web.Navigation.NavigationNodePermissionResolver>(); services.AddScoped <cloudscribe.Web.Navigation.INavigationNodePermissionResolver, cloudscribe.SimpleContent.Web.Services.PagesNavigationNodePermissionResolver>(); services.AddCloudscribeCore(Configuration); services.AddCloudscribeCoreIntegrationForSimpleContent(); services.AddSimpleContent(Configuration); services.AddMetaWeblogForSimpleContent(Configuration.GetSection("MetaWeblogApiOptions")); services.AddSimpleContentRssSyndiction(); services.AddCloudscribeFileManagerIntegration(Configuration); // optional but recommended if you need localization // uncomment to use cloudscribe.Web.localization https://github.com/joeaudette/cloudscribe.Web.Localization //services.Configure<GlobalResourceOptions>(Configuration.GetSection("GlobalResourceOptions")); //services.AddSingleton<IStringLocalizerFactory, GlobalResourceManagerStringLocalizerFactory>(); services.AddLocalization(options => options.ResourcesPath = "GlobalResources"); services.Configure <RequestLocalizationOptions>(options => { var supportedCultures = new[] { new CultureInfo("en-US"), new CultureInfo("en-GB"), new CultureInfo("fr-FR"), new CultureInfo("fr"), }; // State what the default culture for your application is. This will be used if no specific culture // can be determined for a given request. options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US"); // You must explicitly state which cultures your application supports. // These are the cultures the app supports for formatting numbers, dates, etc. options.SupportedCultures = supportedCultures; // These are the cultures the app supports for UI strings, i.e. we have localized resources for. options.SupportedUICultures = supportedCultures; // You can change which providers are configured to determine the culture for requests, or even add a custom // provider with your own logic. The providers will be asked in order to provide a culture for each request, // and the first to provide a non-null result that is in the configured supported cultures list will be used. // By default, the following built-in providers are configured: // - QueryStringRequestCultureProvider, sets culture via "culture" and "ui-culture" query string values, useful for testing // - CookieRequestCultureProvider, sets culture via "ASPNET_CULTURE" cookie // - AcceptLanguageHeaderRequestCultureProvider, sets culture via the "Accept-Language" request header //options.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(async context => //{ // // My custom request culture logic // return new ProviderCultureResult("en"); //})); }); SslIsAvailable = Configuration.GetValue <bool>("AppSettings:UseSsl"); services.Configure <MvcOptions>(options => { if (SslIsAvailable) { options.Filters.Add(new RequireHttpsAttribute()); } options.CacheProfiles.Add("SiteMapCacheProfile", new CacheProfile { Duration = 30 }); options.CacheProfiles.Add("RssCacheProfile", new CacheProfile { Duration = 100 }); }); services.AddRouting(options => { options.LowercaseUrls = true; }); services.AddMvc() .AddRazorOptions(options => { options.AddCloudscribeViewLocationFormats(); options.AddCloudscribeCommonEmbeddedViews(); options.AddCloudscribeNavigationBootstrap3Views(); options.AddCloudscribeCoreBootstrap3Views(); options.AddCloudscribeSimpleContentBootstrap3Views(); options.AddCloudscribeFileManagerBootstrap3Views(); options.AddCloudscribeLoggingBootstrap3Views(); options.ViewLocationExpanders.Add(new cloudscribe.Core.Web.Components.SiteViewLocationExpander()); }) ; }
public void ConfigureServices(IServiceCollection services) { #region 数据库连接 services.AddDbContext <SqlServerDBContext>( options => options.UseSqlServer(Configuration.GetConnectionString("SqlServerConnection"))); #endregion #region 开启razor sdk的运行时编译 services.AddControllersWithViews().AddRazorRuntimeCompilation(); #endregion #region 支持 Razor 页面 services.AddRazorPages(); #endregion services.AddControllers(); #region Swagger配置 services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new OpenApiInfo { Version = "v1.0.0", Title = "WebAPI", Description = "外包电子合同WebApi接口", Contact = new OpenApiContact() { Name = "David Zhou", Email = "*****@*****.**" } }); }); #endregion #region IP限流配置 // 存储IP计数器及配置规则 services.AddMemoryCache(); // 配置 services.Configure <IpRateLimitOptions>(options => { //options.EnableEndpointRateLimiting = true; // true,意思是IP限制会应用于单个配置的Endpoint上,false,只能限制所有接口Endpoint只能为* options.HttpStatusCode = 429; // 触发限制之后给客户端返回的HTTP状态码 options.RealIpHeader = "X-Real-IP"; options.ClientIdHeader = "X-ClientId"; options.QuotaExceededResponse = new QuotaExceededResponse // 触发限制之后给客户端返回的数据 { Content = "{{\"code\":-1,\"msg\":\"访问过于频繁,请稍后重试\"}}", ContentType = "application/json", StatusCode = 429 }; // 限制规则 options.GeneralRules = new List <RateLimitRule> { new RateLimitRule { //Endpoint = "*:/signtoken",// 正则匹配规则,只针对签发token接口 Endpoint = "*", Period = "1s", // 周期 s秒 m分钟 h时 d天 Limit = 10, // 次数 } }; }); // 注入计数器和规则存储 services.AddSingleton <IIpPolicyStore, MemoryCacheIpPolicyStore>(); services.AddSingleton <IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>(); // the clientId/clientIp resolvers use it. services.AddHttpContextAccessor(); // 配置(计数器密钥生成器) services.AddSingleton <IRateLimitConfiguration, RateLimitConfiguration>(); #endregion #region 跨域设置 //注意:放到services.AddMvc()之前 services.AddCors(options => options.AddPolicy("AllowAll", builder => { builder.SetIsOriginAllowed(_ => true) .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials(); })); #endregion #region 添加MVC服务,取消默认驼峰命名 services.AddMvc() .AddJsonOptions(options => { options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All); options.JsonSerializerOptions.PropertyNamingPolicy = null; }); #endregion #region Api 数据格式配置 services.AddControllersWithViews().AddNewtonsoftJson(options => { //忽略循环引用 options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; //使用驼峰样式 options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); //设置时间格式 options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; }); #endregion #region 中间件注册 #region 基础管理 //人员合同服务 services.AddTransient <IPersonService, PersonService>(); //人员附件服务 services.AddTransient <IECPersonFileInfoService, ECPersonFileInfoService>(); //log帮助类 services.AddSingleton <ILoggerHelper, LoggerHelper>(); //log4net repository = LogManager.CreateRepository("WebApp"); //项目名称 XmlConfigurator.Configure(repository, new FileInfo("Log4net.config")); //指定配置文件 #endregion #endregion #region 过滤器 services.AddControllers(option => { //全局异常过滤器 option.Filters.Add(typeof(GlobalExceptionFilter)); }); #endregion }