From d213c87eacc0d16a9fd31da559397cf54bb4b4a5 Mon Sep 17 00:00:00 2001 From: Andy Mantell <134642+andymantell@users.noreply.github.com> Date: Fri, 20 Dec 2024 09:56:24 +0000 Subject: [PATCH] Tightening up cookie settings --- Frontend/CO.CDP.OrganisationApp/Program.cs | 24 +++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/Frontend/CO.CDP.OrganisationApp/Program.cs b/Frontend/CO.CDP.OrganisationApp/Program.cs index 800a8f96a..adb84dada 100644 --- a/Frontend/CO.CDP.OrganisationApp/Program.cs +++ b/Frontend/CO.CDP.OrganisationApp/Program.cs @@ -83,11 +83,33 @@ { options.IdleTimeout = TimeSpan.FromMinutes(sessionTimeoutInMinutes); options.Cookie.IsEssential = true; + options.Cookie.SameSite = SameSiteMode.Strict; + options.Cookie.SecurePolicy = builder.Environment.IsDevelopment() ? CookieSecurePolicy.SameAsRequest : CookieSecurePolicy.Always; }); builder.Services.AddHttpContextAccessor(); builder.Services.AddSingleton(); +builder.Services.ConfigureApplicationCookie(options => +{ + options.Cookie.SameSite = SameSiteMode.Strict; + options.Cookie.SecurePolicy = builder.Environment.IsDevelopment() ? CookieSecurePolicy.SameAsRequest : CookieSecurePolicy.Always; +}); + +builder.Services.AddCookiePolicy(options => +{ + options.MinimumSameSitePolicy = SameSiteMode.Strict; + options.HttpOnly = Microsoft.AspNetCore.CookiePolicy.HttpOnlyPolicy.Always; + options.Secure = builder.Environment.IsDevelopment() ? CookieSecurePolicy.SameAsRequest : CookieSecurePolicy.Always; +}); + +builder.Services.AddAntiforgery(options => +{ + options.Cookie.SecurePolicy = builder.Environment.IsDevelopment() ? CookieSecurePolicy.SameAsRequest : CookieSecurePolicy.Always; + options.Cookie.SameSite = SameSiteMode.Strict; + options.Cookie.HttpOnly = true; +}); + builder.Services.AddTransient(provider => { var httpContextAccessor = provider.GetRequiredService(); @@ -243,11 +265,11 @@ app.UseMiddleware(); app.UseMiddleware(); -// Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error"); app.UseHsts(); + app.UseCookiePolicy(); } app.UseRequestLocalization();