using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ModelBinding.Binders; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.Extensions.DependencyInjection.Extensions; using UCSCommon.HealthChecks; using Newtonsoft.Json.Serialization; using Microsoft.AspNetCore.HttpOverrides; using Rotativa.AspNetCore; using Microsoft.Extensions.DependencyInjection; using UCMS_Surrogates_Rewrite.Web.Security; using UCMS_Surrogates_Rewrite.Web.Util; namespace UCMS_Surrogates_Rewrite.Web { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews().AddRazorRuntimeCompilation(); //Josh added Nov 15 2022 var AuthData = Configuration.GetSection("AuthData"); var AuthDataSettings = AuthData.Get(); services.AddSingleton(_ => AuthDataSettings); services.AddAuthentication(SecurityPermissionHelper.GetAuthenticationSchemeName(AuthDataSettings.ApplicationName)) .AddCookie(SecurityPermissionHelper.GetAuthenticationSchemeName(AuthDataSettings.ApplicationName), options => { options.LoginPath = "/Pages/Login/"; options.ExpireTimeSpan = TimeSpan.FromHours(1);// TimeSpan.FromHours(12); }); services.Configure(options => { options.Cookie.IsEssential = true; }); services.AddAuthorization(options => { options.AddPolicy("CourtUser", policy => { policy.AddAuthenticationSchemes(SecurityPermissionHelper.GetAuthenticationSchemeName(AuthDataSettings.ApplicationName)); policy.RequireAuthenticatedUser(); policy.RequireRole(SecurityHelper.GetPermissionName(SecurityHelper.Permission.Operations)); }); }); //This is so we can access HttpContext Session in our cshtml layout file services.TryAddSingleton(); var devBaseURL = Configuration.GetSection("DevURL").Value; var curBaseURL = devBaseURL; services.AddMemoryCache();//Josh Nov 20 2022 services.AddSession(options => { options.Cookie.HttpOnly = true; options.Cookie.IsEssential = true; options.IdleTimeout = TimeSpan.MaxValue; }); services.AddHttpContextAccessor(); services.AddKendo(); var ConnectionInfo = Configuration.GetSection("ConnectionInfo"); var ConnectionSettings = ConnectionInfo.Get(); services.AddSingleton(_ => ConnectionSettings); services.AddBackgroundHealthChecks(); //Dependency Injection Stuff var VSCMSConnectionSection = Configuration.GetSection("VSCMSConnectionInfo"); services.Configure(VSCMSConnectionSection); var HCaptchSection = Configuration.GetSection("HCaptcha"); var HCaptchaObject = HCaptchSection.Get(); services.AddSingleton(_ => HCaptchaObject); services.AddMvc(); services.AddMvc() .AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver()); services.AddRazorPages().AddRazorPagesOptions(options => { options.Conventions.AddPageRoute("/Login/Login",""); options.Conventions.AllowAnonymousToFolder("/Shared"); options.Conventions.AllowAnonymousToFolder("/Login"); //options.Conventions.AuthorizeFolder("/Appearances", "Appearances"); options.Conventions.AllowAnonymousToFolder("/Appearances"); //For now until we actually get the login set up }); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/ErrorView"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } // Configure Forwarded Headers var forwardedHeadersOptions = new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost }; forwardedHeadersOptions.KnownNetworks.Clear(); // Clear default networks forwardedHeadersOptions.KnownProxies.Clear(); // Clear default proxies app.UseForwardedHeaders(forwardedHeadersOptions); app.UseCookiePolicy(); app.UseHttpsRedirection(); app.UseDefaultFiles(); app.UseStaticFiles(); app.UseSession(); app.UseAuthentication(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); endpoints.MapControllers(); endpoints.MapBackgroundHealthChecksWithOcaDefaults(); }); RotativaConfiguration.Setup(env.WebRootPath, "Rotativa"); } } }