Security and Identity - SameSite cookies
In this article
SameSite
is an IETF draft standard designed to provide some protection against cross-site request forgery (CSRF) attacks. Originally drafted in 2016, the draft standard was updated in 2019. The updated standard is not backward compatible with the previous standard, with the following being the most noticeable differences:
-
Cookies without
SameSite
header are treated asSameSite=Lax
by default. -
SameSite=None
must be used to allow cross-site cookie use. -
Cookies that assert
SameSite=None
must also be marked asSecure
. -
Applications that use
<iframe>
may experience issues withsameSite=Lax
orsameSite=Strict
cookies because<iframe>
is treated as cross-site scenarios. -
The value
SameSite=None
is not allowed by the 2016 standard and causes some implementations to treat such cookies asSameSite=Strict
. See Supporting older browsers in this document.
The SameSite=Lax
setting works for most application cookies. Some forms of authentication like OpenID Connect (OIDC) and WS-Federation default to POST based redirects. The POST based redirects trigger the SameSite
browser protections, so SameSite
is disabled for these components. Most OAuth logins are not affected due to differences in how the request flows.
Each ASP.NET Core component that emits cookies needs to decide if SameSite
is appropriate.
SameSite
and Identity
ASP.NET Core Identity
is largely unaffected by SameSite
cookies except for advanced scenarios like IFrames
or OpenIdConnect
integration.
When using Identity
, do not add any cookie providers or call services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
, Identity
takes care of that.
SameSite
test sample code
Sample | Document |
---|---|
.NET Core Razor Pages | ASP.NET Core 3.1 Razor Pages ```SameSite``` cookie sample |
.NET Core support for the sameSite attribute
var cookieOptions = new CookieOptions
{
// Set the secure flag, which Chrome's changes will require for SameSite none.
// Note this will also require you to be running on HTTPS.
Secure = true,
// Set the cookie to HTTP only which is good practice unless you really do need
// to access it client side in scripts.
HttpOnly = true,
// Add the SameSite attribute, this will emit the attribute with a value of none.
SameSite = SameSiteMode.None
// The client should follow its default cookie policy.
// SameSite = SameSiteMode.Unspecified
};
// Add the cookie to the response cookie collection
Response.Cookies.Append("MyCookie", "cookieValue", cookieOptions);
}
SameSite
API usage with HttpContext.Response.Cookies.Append(
"name", "value",
new CookieOptions() { SameSite = SameSiteMode.Lax });
-
Redefines the behavior of
SameSiteMode.None
to emitSameSite=None
-
Adds a new value
SameSiteMode.Unspecified
to omit theSameSite
attribute. -
All cookies APIs default to
Unspecified
. Some components that use cookies set values more specific to their scenarios. See the table above for examples. -
CookieOptions
used with HttpContext.Response.Cookies.Append -
CookieBuilder used as a factory for
CookieOptions
-
CookiePolicyOptions.MinimumSameSitePolicy
History and changes
-
Is not backwards compatible with the 2016 draft. For more information, see Supporting older browsers in this document.
-
Specifies cookies are treated as
SameSite=Lax
by default. -
Specifies cookies that explicitly assert
SameSite=None
in order to enable cross-site delivery should be marked asSecure
.None
is a new entry to opt out. -
Is supported by patches issued for ASP.NET Core 2.1, 2.2, and 3.0. ASP.NET Core 3.1 and later has additional
SameSite
support. -
Is scheduled to be enabled by Chrome by default in Feb 2020. Browsers started moving to this standard in 2019.
SameSite
draft standard to the 2019 draft standard
APIs impacted by the change from the 2016 -
Http.SameSiteMode
-
CookieOptions.SameSite
-
CookieBuilder.SameSite
-
CookiePolicyOptions.MinimumSameSitePolicy
-
Microsoft.Net.Http.Headers.SameSiteMode
-
Microsoft.Net.Http.Headers.SetCookieHeaderValue.SameSite
Supporting older browsers
var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<CookiePolicyOptions>(options =>
{
options.MinimumSameSitePolicy = SameSiteMode.Unspecified;
options.OnAppendCookie = cookieContext =>
CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
options.OnDeleteCookie = cookieContext =>
CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
});
void CheckSameSite(HttpContext httpContext, CookieOptions options)
{
if (options.SameSite == SameSiteMode.None)
{
var userAgent = httpContext.Request.Headers["User-Agent"].ToString();
if (MyUserAgentDetectionLib.DisallowsSameSiteNone(userAgent))
{
options.SameSite = SameSiteMode.Unspecified;
}
}
}
builder.Services.AddRazorPages();
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseCookiePolicy();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<CookiePolicyOptions>(options =>
{
options.MinimumSameSitePolicy = SameSiteMode.Unspecified;
options.OnAppendCookie = cookieContext =>
CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
options.OnDeleteCookie = cookieContext =>
CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
});
void CheckSameSite(HttpContext httpContext, CookieOptions options)
{
if (options.SameSite == SameSiteMode.None)
{
var userAgent = httpContext.Request.Headers["User-Agent"].ToString();
if (MyUserAgentDetectionLib.DisallowsSameSiteNone(userAgent))
{
options.SameSite = SameSiteMode.Unspecified;
}
}
}
builder.Services.AddRazorPages();
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseCookiePolicy();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
if (MyUserAgentDetectionLib.DisallowsSameSiteNone(userAgent))
{
options.SameSite = SameSiteMode.Unspecified;
}
Warning The following code is for demonstration only:
It should not be considered complete. It is not maintained or supported.
-
It should not be considered complete.
-
It is not maintained or supported.
public static bool DisallowsSameSiteNone(string userAgent)
{
// Check if a null or empty string has been passed in, since this
// will cause further interrogation of the useragent to fail.
if (String.IsNullOrWhiteSpace(userAgent))
return false;
// Cover all iOS based browsers here. This includes:
// - Safari on iOS 12 for iPhone, iPod Touch, iPad
// - WkWebview on iOS 12 for iPhone, iPod Touch, iPad
// - Chrome on iOS 12 for iPhone, iPod Touch, iPad
// All of which are broken by SameSite=None, because they use the iOS networking
// stack.
if (userAgent.Contains("CPU iPhone OS 12") ||
userAgent.Contains("iPad; CPU OS 12"))
{
return true;
}
// Cover Mac OS X based browsers that use the Mac OS networking stack.
// This includes:
// - Safari on Mac OS X.
// This does not include:
// - Chrome on Mac OS X
// Because they do not use the Mac OS networking stack.
if (userAgent.Contains("Macintosh; Intel Mac OS X 10_14") &&
userAgent.Contains("Version/") && userAgent.Contains("Safari"))
{
return true;
}
// Cover Chrome 50-69, because some versions are broken by SameSite=None,
// and none in this range require it.
// Note: this covers some pre-Chromium Edge versions,
// but pre-Chromium Edge does not require SameSite=None.
if (userAgent.Contains("Chrome/5") || userAgent.Contains("Chrome/6"))
{
return true;
}
return false;
}
SameSite
problems
Test apps for -
Test the interaction on multiple browsers.
-
Apply the CookiePolicy browser detection and mitigation discussed in this document.
Test with Chrome
-
Chromium 76 Win64
-
Chromium 74 Win64
Test with Safari
Test with Firefox
Test with Edge browser
Test with Edge (Chromium)
Test with Electron
Additional resources
-
Chromium Blog:Developers: Get Ready for New
SameSite=None
;Secure
Cookie Settings -
SameSite
cookies explained -
November 2019 Patches
Sample | Document |
---|---|
.NET Core Razor Pages | ASP.NET Core 3.1 Razor Pages ```SameSite``` cookie sample |