// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseForwardedHeaders(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/error/500"); app.UseHsts(); } // Do not write telemetry to debug output TelemetryDebugWriter.IsTracingDisabled = true; app.UseResponseCaching(); app.UseResponseCompression(); app.UseStaticFiles(); app.UseCookiePolicy(); app.UseRouting(); app.UseAuthentication(); // WorkContextBuildMiddleware must always be registered first in the Middleware chain app.UseMiddleware <WorkContextBuildMiddleware>(); app.UseMiddleware <StoreMaintenanceMiddleware>(); app.UseMiddleware <NoLiquidThemeMiddleware>(); app.UseMiddleware <CreateStorefrontRolesMiddleware>(); app.UseMiddleware <ApiErrorHandlingMiddleware>(); var mvcJsonOptions = app.ApplicationServices.GetService <IOptions <MvcNewtonsoftJsonOptions> >().Value; mvcJsonOptions.SerializerSettings.Converters.Add(new CartTypesJsonConverter(app.ApplicationServices.GetService <IWorkContextAccessor>())); mvcJsonOptions.SerializerSettings.Converters.Add(new MoneyJsonConverter(app.ApplicationServices.GetService <IWorkContextAccessor>())); mvcJsonOptions.SerializerSettings.Converters.Add(new CurrencyJsonConverter(app.ApplicationServices.GetService <IWorkContextAccessor>())); mvcJsonOptions.SerializerSettings.Converters.Add(new OrderTypesJsonConverter(app.ApplicationServices.GetService <IWorkContextAccessor>())); mvcJsonOptions.SerializerSettings.Converters.Add(new RecommendationJsonConverter(app.ApplicationServices.GetService <IRecommendationProviderFactory>())); var mvcViewOptions = app.ApplicationServices.GetService <IOptions <MvcViewOptions> >().Value; mvcViewOptions.ViewEngines.Add(app.ApplicationServices.GetService <ILiquidViewEngine>()); // Do not use status code pages for Api requests app.UseWhen(context => !context.Request.Path.IsApi(), appBuilder => { appBuilder.UseStatusCodePagesWithReExecute("/error/{0}"); }); // Enable middleware to serve generated Swagger as a JSON endpoint. app.UseSwagger(c => c.RouteTemplate = "docs/{documentName}/docs.json"); var swaggerOptions = app.ApplicationServices.GetService <IOptions <SwaggerOptions> >().Value; if (swaggerOptions.UI.Enable) { app.UseSwaggerUI(c => { c.SwaggerEndpoint($"./{SwaggerDocName}/docs.json", SwaggerDocName); c.RoutePrefix = "docs"; c.EnableValidator(); c.IndexStream = () => { var type = typeof(Startup).GetTypeInfo().Assembly .GetManifestResourceStream("VirtoCommerce.Storefront.wwwroot.swagger.index.html"); return(type); }; c.DocumentTitle = "VirtoCommerce Storefront REST API documentation"; c.InjectStylesheet("/swagger/vc.css"); c.ShowExtensions(); c.DocExpansion(DocExpansion.None); c.DefaultModelsExpandDepth(-1); }); } var rewriteOptions = new RewriteOptions(); // Load IIS url rewrite rules from external file if (File.Exists("IISUrlRewrite.xml")) { using (var iisUrlRewriteStreamReader = File.OpenText("IISUrlRewrite.xml")) { rewriteOptions.AddIISUrlRewrite(iisUrlRewriteStreamReader); } } rewriteOptions.Add(new StorefrontUrlNormalizeRule()); var requireHttpsOptions = new RequireHttpsOptions(); Configuration.GetSection("VirtoCommerce:RequireHttps").Bind(requireHttpsOptions); if (requireHttpsOptions.Enabled) { rewriteOptions.AddRedirectToHttps(requireHttpsOptions.StatusCode, requireHttpsOptions.Port); } app.UseRewriter(rewriteOptions); // Enable browser XSS protection app.Use(async(context, next) => { context.Response.Headers["X-Xss-Protection"] = "1"; await next(); }); // It will be good to rewrite endpoint routing as described here, but it's not easy to do: // https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio#routing-startup-code app.UseMvc(routes => { routes.MapSlugRoute("{*path}", defaults: new { controller = "Home", action = "Index" }); }); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/error/500"); app.UseHsts(); } //Do not write telemetry to debug output TelemetryDebugWriter.IsTracingDisabled = true; app.UseResponseCaching(); app.UseResponseCompression(); app.UseStaticFiles(); app.UseCookiePolicy(); app.UseAuthentication(); //WorkContextBuildMiddleware must always be registered first in the Middleware chain app.UseMiddleware <WorkContextBuildMiddleware>(); app.UseMiddleware <StoreMaintenanceMiddleware>(); app.UseMiddleware <NoLiquidThemeMiddleware>(); app.UseMiddleware <CreateStorefrontRolesMiddleware>(); app.UseMiddleware <ApiErrorHandlingMiddleware>(); app.UseStatusCodePagesWithReExecute("/error/{0}"); // Enable middleware to serve generated Swagger as a JSON endpoint. app.UseSwagger(c => c.RouteTemplate = "docs/{documentName}/docs.json"); var rewriteOptions = new RewriteOptions(); //Load IIS url rewrite rules from external file if (File.Exists("IISUrlRewrite.xml")) { using (var iisUrlRewriteStreamReader = File.OpenText("IISUrlRewrite.xml")) { rewriteOptions.AddIISUrlRewrite(iisUrlRewriteStreamReader); } } rewriteOptions.Add(new StorefrontUrlNormalizeRule()); var requireHttpsOptions = new RequireHttpsOptions(); Configuration.GetSection("VirtoCommerce:RequireHttps").Bind(requireHttpsOptions); if (requireHttpsOptions.Enabled) { rewriteOptions.AddRedirectToHttps(requireHttpsOptions.StatusCode, requireHttpsOptions.Port); } app.UseRewriter(rewriteOptions); //Enable browser XSS protection app.Use(async(context, next) => { context.Response.Headers["X-Xss-Protection"] = "1"; await next(); }); app.UseMvc(routes => { routes.MapSlugRoute("{*path}", defaults: new { controller = "Home", action = "Index" }); }); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddGoogle(GetProjectId()); loggerFactory.AddDebug(); // Configure redirects to HTTPS. var rewriteOptions = new RewriteOptions(); if (Configuration["IAmRunningInGoogleCloud"] == "true") { rewriteOptions.Add(new RewriteHttpsOnAppEngine( HttpsPolicy.Required)); } else { rewriteOptions.AddRedirectToHttps(302, 44393); } app.UseRewriter(rewriteOptions); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); app.UseBrowserLink(); } else { app.UseExceptionHandler("/Home/Error"); app.UseGoogleExceptionLogging(); app.UseGoogleTrace(); } app.UseStaticFiles(); app.UseIdentity(); int authenticationProviderCount = 0; // Add external authentication middleware below. To configure them // please see http://go.microsoft.com/fwlink/?LinkID=532715 string googleClientId = Configuration["Authentication:Google:ClientId"]; if (!string.IsNullOrWhiteSpace(googleClientId)) { app.UseGoogleAuthentication(new GoogleOptions() { ClientId = googleClientId, ClientSecret = Configuration[ "Authentication:Google:ClientSecret"], }); authenticationProviderCount += 1; } string facebookAppId = Configuration["Authentication:Facebook:AppId"]; if (!string.IsNullOrWhiteSpace(facebookAppId)) { app.UseFacebookAuthentication(new FacebookOptions() { AppId = facebookAppId, AppSecret = Configuration[ "Authentication:Facebook:AppSecret"], }); authenticationProviderCount += 1; } if (0 == authenticationProviderCount) { app.Run(RequireAuthenticationProviderHandler); } app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); }
public static RewriteOptions AddRedirectToWWwIfPasswordlessDomain(this RewriteOptions options) { options.Add(new RedirectToWWwIfPasswordlessDomainRule()); return(options); }
public static void AddRedirectRules(this IApplicationBuilder app) { var options = new RewriteOptions(); options.Add(new PathWithQueryRule(@"(?i)apprentice\b\/(starting-apprenticeship|your-apprenticeship)", "/apprentices/starting-apprenticeship")); options.AddRedirect(@"(?i)apprentice\b\/(starting-apprenticeship|your-apprenticeship)", "/apprentices/starting-apprenticeship", (int)HttpStatusCode.PermanentRedirect); options.Add(new PathWithQueryRule(@"(?i)apprentice\b\/(interview)", "/apprentices/interview-process")); options.AddRedirect(@"(?i)apprentice\b\/(interview)", "/apprentices/interview-process", (int)HttpStatusCode.PermanentRedirect); options.Add(new PathWithQueryRule(@"(?i)apprentice\b\/(application|application-process)", "/apprentices/applying-apprenticeship")); options.AddRedirect(@"(?i)apprentice\b\/(application|application-process)", "/apprentices/applying-apprenticeship", (int)HttpStatusCode.PermanentRedirect); options.Add(new PathWithQueryRule(@"(?i)apprentice\b\/(what-is-apprenticeship|what-is-an-apprenticeship)", "/apprentices/becoming-apprentice")); options.AddRedirect(@"(?i)apprentice\b\/(what-is-apprenticeship|what-is-an-apprenticeship)", "/apprentices/becoming-apprentice", (int)HttpStatusCode.PermanentRedirect); options.Add(new PathWithQueryRule(@"(?i)apprentice\b\/(what-are-the-benefits-for-me)", "/apprentices/benefits-apprenticeship")); options.AddRedirect(@"(?i)apprentice\b\/(what-are-the-benefits-for-me)", "/apprentices/benefits-apprenticeship", (int)HttpStatusCode.PermanentRedirect); options.Add(new PathWithQueryRule(@"(?i)apprentice\b\/(assessment-and-certification)", "/apprentices/assessment-and-certification")); options.AddRedirect(@"(?i)apprentice\b\/(assessment-and-certification)", "/apprentices/assessment-and-certification", (int)HttpStatusCode.PermanentRedirect); options.Add(new PathWithQueryRule(@"(?i)apprentice\b\/(find-an-apprenticeship)", "/apprentices/browse-apprenticeships")); options.AddRedirect(@"(?i)apprentice\b\/(find-an-apprenticeship)", "/apprentices/browse-apprenticeships", (int)HttpStatusCode.PermanentRedirect); options.Add(new PathWithQueryRule(@"(?i)employer\b\/(how-much-is-it-going-to-cost|funding-an-apprenticeship)", "/employers/funding-an-apprenticeship")); options.AddRedirect(@"(?i)employer\b\/(how-much-is-it-going-to-cost|funding-an-apprenticeship)", "/employers/funding-an-apprenticeship", (int)HttpStatusCode.PermanentRedirect); options.Add(new PathWithQueryRule(@"(?i)employer\b\/(the-right-apprenticeship)", "/employers/choose-apprenticeship-training")); options.AddRedirect(@"(?i)employer\b\/(the-right-apprenticeship)", "/employers/choose-apprenticeship-training", (int)HttpStatusCode.PermanentRedirect); options.Add(new PathWithQueryRule(@"(?i)employer\b\/(choose-training-provider)", "/employers/choose-training-provider")); options.AddRedirect(@"(?i)employer\b\/(choose-training-provider)", "/employers/choose-training-provider", (int)HttpStatusCode.PermanentRedirect); options.Add(new PathWithQueryRule(@"(?i)employer\b\/(hire-an-apprentice|hiring-an-apprentice)", "/employers/hiring-an-apprentice")); options.AddRedirect(@"(?i)employer\b\/(hire-an-apprentice|hiring-an-apprentice)", "/employers/hiring-an-apprentice", (int)HttpStatusCode.PermanentRedirect); options.Add(new PathWithQueryRule(@"(?i)employer\b\/(end-point-assessments|assessment-and-certification)", "/employers/end-point-assessments")); options.AddRedirect(@"(?i)employer\b\/(end-point-assessments|assessment-and-certification)", "/employers/end-point-assessments", (int)HttpStatusCode.PermanentRedirect); options.Add(new PathWithQueryRule(@"(?i)employer\b\/(benefits)", "/employers/benefits-of-hiring-apprentice")); options.AddRedirect(@"(?i)employer\b\/(benefits)", "/employers/benefits-of-hiring-apprentice", (int)HttpStatusCode.PermanentRedirect); options.Add(new PathWithQueryRule(@"(?i)employer\b\/(training-your-apprentice)", "/employers/training-your-apprentice")); options.AddRedirect(@"(?i)employer\b\/(training-your-apprentice)", "/employers/training-your-apprentice", (int)HttpStatusCode.PermanentRedirect); options.Add(new PathWithQueryRule(@"(?i)employer\b\/(upskill)", "/employers/upskilling-your-workforce")); options.AddRedirect(@"(?i)employer\b\/(upskill)", "/employers/upskilling-your-workforce", (int)HttpStatusCode.PermanentRedirect); options.Add(new PathWithQueryRule(@"(?i)employers\b\/(upskill)", "/employers/upskilling-your-workforce")); options.AddRedirect(@"(?i)employer\b\/(upskill)", "/employers/upskilling-your-workforce", (int)HttpStatusCode.PermanentRedirect); options.Add(new PathWithQueryRule(@"(?i)real\-stories\b\/(apprentice)", "/apprentices/real-stories")); options.AddRedirect(@"(?i)real\-stories\b\/(apprentice)$", "/apprentices/real-stories", (int)HttpStatusCode.PermanentRedirect); options.Add(new PathWithQueryRule(@"(?i)apprentice\b\/(real\-stories)$", "/apprentices/real-stories")); options.AddRedirect(@"(?i)apprentice\b\/(real\-stories)$", "/apprentices/real-stories", (int)HttpStatusCode.PermanentRedirect); options.Add(new PathWithQueryRule(@"(?i)real\-stories\b\/(employer)$", "/employers/real-stories-employers")); options.AddRedirect(@"(?i)real\-stories\b\/(employer)$", "/employers/real-stories-employers", (int)HttpStatusCode.PermanentRedirect); options.Add(new PathWithQueryRule(@"(?i)employer\b\/(real\-stories)$", "/employers/real-stories-employers")); options.AddRedirect(@"(?i)employer\b\/(real\-stories)$", "/employers/real-stories-employers", (int)HttpStatusCode.PermanentRedirect); options.Add(new PathWithQueryRule(@"(?i)employers\b\/(real\-stories)$", "/employers/real-stories-employers")); options.AddRedirect(@"(?i)employers\b\/(real\-stories)$", "/employers/real-stories-employers", (int)HttpStatusCode.PermanentRedirect); options.Add(new PathWithQueryRule(@"(?i)parents\b\/(their\-career)$", "/apprentices/help-shape-their-career")); options.AddRedirect(@"(?i)parents\b\/(their\-career)$", "/apprentices/help-shape-their-career", (int)HttpStatusCode.PermanentRedirect); app.UseRewriter(options); }