using System.Collections.Generic; using System.Linq; using System.Xml.Linq; using CTA.Rules.Models; namespace CTA.Rules.Actions { public static class ServerConfigTemplates { // Middleware component registered should be injected in this Order, Add new attribute at the correponding position internal static List ConfigAttributes = new List { "HttpCompression", "HttpRedirect", "Handlers", "Modules", "Security" }; internal static List CompresionTypes = new List { "staticTypes", "dynamicTypes" }; internal static Dictionary HttpResponseStatus = new Dictionary() { {"Found", 302}, {"Permanent", 301 }, {"Temporary", 307 }, {"PermRedirect", 308 } }; internal static Dictionary> DefaultPreMiddleWareTemplates = new Dictionary>() { {ProjectType.WebApi, new List{ @"if(env.IsDevelopment()){app.UseDeveloperExceptionPage();}", "app.UseHttpsRedirection();" } }, {ProjectType.Mvc, new List{@"if(env.IsDevelopment()){app.UseDeveloperExceptionPage();}else{app.UseExceptionHandler(""/Home/Error"");}", "app.UseHttpsRedirection();", "app.UseStaticFiles();" } } }; internal static Dictionary> DefaultPostMiddleWareTemplates = new Dictionary>() { {ProjectType.Mvc, new List{ @"app.UseRouting();", "app.UseAuthorization();", @"app.UseEndpoints(endpoints =>{endpoints.MapControllerRoute(name: ""default"",pattern: ""{controller=Home}/{action=Index}/{id?}"");});" } }, {ProjectType.WebApi, new List{ @"app.UseRouting();", "app.UseAuthorization();", "app.UseEndpoints(endpoints =>{endpoints.MapControllers();});" } } }; internal static Dictionary> DefaultServiceExpressionTemplates = new Dictionary>() { {ProjectType.Mvc, new List{ "services.AddControllersWithViews();" } }, {ProjectType.WebApi, new List{ @"services.AddControllers();" } } }; internal const string ConfigureServicesMethod = "ConfigureServices"; internal const string ConfigureMiddlewareMethod = "Configure"; internal const string ConfigureHostBuilderMethod = "CreateHostBuilder"; internal const string LambdaWebBuilderAttribute = "webBuilder"; internal static string kestrelTemplate = @"webBuilder.UseKestrel(options =>{kestrel_options});"; internal static string addRedirectTemplate = @".AddRedirect(@""{0}"", ""{1}"", {2})"; internal static List MiddlewareConfigMethods = new List() { ConfigureServicesMethod, ConfigureMiddlewareMethod }; internal static Dictionary ConfigurationExpressionTemplates = new Dictionary() { {WebServerConfigAttributes.HttpCompression.ToString(), "app.UseResponseCaching();"}, {WebServerConfigAttributes.Authentication.ToString(), "app.UseAuthentication();"}, {WebServerConfigAttributes.Authorization.ToString(), "app.UseAuthorization();" }, {WebServerConfigAttributes.Modules.ToString(),"app.UseMiddleware<{0}>();"}, {WebServerConfigAttributes.Handlers.ToString(), @"app.MapWhen(context => context.Request.Path.ToString().EndsWith(""{0}""),appBranch => {{ appBranch.UseMiddleware<{1}>();}});" }, {WebServerConfigAttributes.HttpRedirect.ToString(),"app.UseRewriter({0});"}, }; internal static Dictionary ServiceExpressionTemplates = new Dictionary() { {WebServerConfigAttributes.HttpCompression.ToString(), @"services.AddResponseCompression(options =>{options.Providers.Add();options.Providers.Add(); options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[] { mime_types });}); "}, {WebServerConfigAttributes.Authorization.ToString(), @"services.AddAuthorization(options =>{options.FallbackPolicy = options.DefaultPolicy;});"}, {WebServerConfigAttributes.WindowsAuthentication.ToString(), @"services.AddAuthentication(NegotiateDefaults.AuthenticationScheme).AddNegotiate(options =>{options.Events = new NegotiateEvents(){OnAuthenticationFailed = context => { return Task.CompletedTask}};});" } }; internal static Dictionary KestrelOptionsTemplates = new Dictionary() { {WebServerConfigAttributes.RequestLimits.ToString(), @"options.Limits.MaxRequestBodySize = {0};"}, }; internal static Dictionary> Directives = new Dictionary>() { {WebServerConfigAttributes.WindowsAuthentication.ToString(), new List(){"Microsoft.AspNetCore.Authentication.Negotiate"}}, {WebServerConfigAttributes.HttpCompression.ToString(), new List(){"Microsoft.AspNetCore.ResponseCompression"}}, {WebServerConfigAttributes.HttpRedirect.ToString(), new List{ "Microsoft.AspNetCore.Rewrite"}} }; internal static List AdditonalComments = new List() { "If certs are not provided for deployment communication will be on http, please remove the https section of the kestrel config in appsettings.json and also remove middleware component app.UseHttpsRedirection();" }; internal static bool HasAny(this IEnumerable element) { return element != null && element.Any(); } // these handlers are specific to system.web, which is not supported in .net core internal static HashSet UnsupportedHandlers = new HashSet { "System.Web.Handlers.PrecompHandler", "System.Web.Handlers.AssemblyResourceLoader", "System.Web.Handlers.TraceHandler", "System.Web.Handlers.TransferRequestHandler", "System.Web.Handlers.WebPartExportHandler" }; internal static string DefaultKestrelHttpConfig = @"{""Endpoints"": {""Http"": {""Url"": ""http://0.0.0.0:80""},""Https"": {""Url"": ""https://0.0.0.0:443"",""Certificate"": {""Path"": """",""Password"": """"}}}}"; } }