Skip to content

Commit

Permalink
Tightening up cookie settings
Browse files Browse the repository at this point in the history
  • Loading branch information
andymantell committed Dec 20, 2024
1 parent 03e9101 commit d213c87
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion Frontend/CO.CDP.OrganisationApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ISession, Session>();

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<IHttpContextAccessor>();
Expand Down Expand Up @@ -243,11 +265,11 @@
app.UseMiddleware<ExceptionMiddleware>();
app.UseMiddleware<CookieAcceptanceMiddleware>();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
app.UseCookiePolicy();
}

app.UseRequestLocalization();
Expand Down

0 comments on commit d213c87

Please sign in to comment.