public async Task SetLocalAppData_ShouldRead() { var appDataFolderFullPath = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "setup_app_settings_test"); _hostStorage.CreateDirectory(appDataFolderFullPath); var path = Path.Combine(appDataFolderFullPath, "appsettings.json"); var example = new PlainTextFileHelper().StringToStream( "{\n \"app\" :{\n \"isAccountRegisterOpen\": \"true\"\n }\n}\n"); await _hostStorage.WriteStreamAsync(example, path); Environment.SetEnvironmentVariable("app__appsettingspath", path); var builder = await SetupAppSettings.AppSettingsToBuilder(); var services = new ServiceCollection(); var appSettings = SetupAppSettings.ConfigurePoCoAppSettings(services, builder); Assert.IsFalse(string.IsNullOrEmpty(appSettings.AppSettingsPath)); Assert.IsTrue(appSettings.IsAccountRegisterOpen); Assert.AreEqual(path, appSettings.AppSettingsPath); _hostStorage.FolderDelete(appDataFolderFullPath); Environment.SetEnvironmentVariable("app__appsettingspath", null); }
public async Task SetLocalAppData_ShouldTakeDefault() { Environment.SetEnvironmentVariable("app__appsettingspath", null); var builder = await SetupAppSettings.AppSettingsToBuilder(); var services = new ServiceCollection(); var appSettings = SetupAppSettings.ConfigurePoCoAppSettings(services, builder); var expectedPath = Path.Combine(appSettings.BaseDirectoryProject, "appsettings.patch.json"); Assert.AreEqual(expectedPath, appSettings.AppSettingsPath); Assert.IsFalse(appSettings.IsAccountRegisterOpen); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { _appSettings = SetupAppSettings.ConfigurePoCoAppSettings(services, _configuration); // before anything else EnableCompression(services); services.AddMemoryCache(); // this is ignored here: appSettings.AddMemoryCache; but implemented in cache // Detect Application Insights (used in next SetupDatabaseTypes) services.AddMonitoring(_appSettings); // LoggerFactory services.AddApplicationInsightsLogging(_appSettings); var foundationDatabaseName = typeof(ApplicationDbContext).Assembly.FullName?.Split(",").FirstOrDefault(); new SetupDatabaseTypes(_appSettings, services).BuilderDb(foundationDatabaseName); new SetupHealthCheck(_appSettings, services).BuilderHealth(); // Enable Dual Authentication services .AddAuthentication(sharedOptions => { sharedOptions.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; sharedOptions.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme; }) .AddCookie(options => { options.Cookie.Name = "_id"; options.ExpireTimeSpan = TimeSpan.FromDays(60); options.SlidingExpiration = false; options.Cookie.HttpOnly = true; options.Cookie.IsEssential = true; options.Cookie.Path = "/"; options.Cookie.SameSite = SameSiteMode.Lax; // allow links from non-domain sites options.LoginPath = "/account/login"; options.LogoutPath = "/account/logout"; options.Events.OnRedirectToLogin = ReplaceReDirector(HttpStatusCode.Unauthorized, options.Events.OnRedirectToLogin); } ); // There is a base-cookie and in index controller there is an method to generate a token that is used to send with the header: X-XSRF-TOKEN services.AddAntiforgery( options => { options.Cookie.Name = "_af"; options.Cookie.HttpOnly = true; // only used by .NET, there is a separate method to generate a X-XSRF-TOKEN cookie options.Cookie.SameSite = SameSiteMode.Lax; options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest; options.Cookie.Path = "/"; options.Cookie.IsEssential = true; options.HeaderName = "X-XSRF-TOKEN"; } ); // to add support for swagger new SwaggerSetupHelper(_appSettings).Add01SwaggerGenHelper(services); // Now only for dev services.AddCors(options => { options.AddPolicy("CorsDevelopment", builder => builder .WithOrigins("http://localhost:4200", "http://localhost:8080") .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials()); options.AddPolicy("CorsProduction", builder => builder .AllowCredentials()); }); services.AddMvcCore().AddApiExplorer().AddAuthorization().AddViews(); ConfigureForwardedHeaders(services); // Application Insights services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); RegisterDependencies.Configure(services); // Reference due missing links between services services.AddSingleton <PackageTelemetryBackgroundService>(); }