The cookie '.AspNetCore.Cookies' has set 'SameSite=None' and must also set
'Secure'. is a warning ASP.NET Core writes to the server log when it sends a cookie
marked SameSite=None without the Secure flag. If nothing
cross-site needs the cookie, remove the SameSite = SameSiteMode.None line; if
something does, add Cookie.SecurePolicy = CookieSecurePolicy.Always and serve
the site over HTTPS.
The warning does not stop the response: in the test the sign-in succeeded and the cookie
was sent. What it reports is the combination of SameSite=None and a missing
Secure flag on the same cookie. SameSite=None is the setting that
lets a browser send the cookie with requests started by other sites; that is background
from the cookie rules, not something this test ran in a browser.
The error
warn: Microsoft.AspNetCore.Http.ResponseCookies[1]
The cookie '.AspNetCore.Cookies' has set 'SameSite=None' and must also set 'Secure'.
It appeared in the server console at the first sign-in, while the response itself was a normal 200 with this header:
Set-Cookie: .AspNetCore.Cookies=CfDJ8...; path=/; samesite=none; httponly
Why it happens
The test app configured cookie authentication with
options.Cookie.SameSite = SameSiteMode.None and ran over plain
http://127.0.0.1:15610. The cookie's SecurePolicy was left at
its default, and on an http request the cookie went out without secure, as the
header above shows. The warning came on the same request, the sign-in that set the
cookie, and it names the cookie: .AspNetCore.Cookies is the default name of the
authentication cookie in this app.
The fix
...
if (builder.Configuration["SameSiteNone"] == "true")
{
options.Cookie.SameSite = SameSiteMode.None;
}
if (builder.Configuration["SecureAlways"] == "true")
{
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
}
...
PS> curl.exe -s -i -c jar2.txt "http://127.0.0.1:15610/login?user=maria"
HTTP/1.1 200 OK
...
Set-Cookie: .AspNetCore.Cookies=CfDJ8...; path=/; secure; samesite=none; httponly
...
Signed in as maria.
With SecurePolicy set to Always the header gained
secure and the server console printed no warning. The two settings are behind
command-line switches only because the test needed both versions; in an app they are two
plain lines in AddCookie. The sign-in still worked afterwards, checked with
/me over http and over https:
PS> curl.exe -s -b jar3.txt http://127.0.0.1:15610/me
You are maria.
...
PS> curl.exe -s -b jar4.txt https://127.0.0.1:15613/me
You are maria.
Over http, curl 8.22.0 still sent the secure cookie back to 127.0.0.1, so this
test does not show what a browser does with a secure cookie on an http page; run the site
on HTTPS. Removing the SameSite = SameSiteMode.None line is the simpler fix
when nothing cross-site needs the cookie: the earlier sign-ins of the same app, with default
cookie options, printed no such warning.
How it was reproduced
A project from dotnet new web -n AfLab -f net10.0 on .NET SDK 11.0.100, no
NuGet packages, running on ASP.NET Core 10.0.15 in Development on 127.0.0.1:15610, with
AddAuthentication().AddCookie() and a test-only GET /login?user=
endpoint that calls SignInAsync without a password. Requests went through
curl 8.22.0; the user name is the test's own.
Frequently asked
- How do I fix has set SameSite=None and must also set Secure in ASP.NET Core?
- Remove the SameSite None line if nothing cross-site needs the cookie; otherwise set options.Cookie.SecurePolicy = CookieSecurePolicy.Always and serve the site over HTTPS. In the test the Set-Cookie header gained secure and the warning was gone.
- Does the SameSite=None must also set Secure warning break login?
- The server still answered: in the test the sign-in returned 200 with the cookie, and logged the warning; after the fix, curl requests with the cookie were recognised as signed in. No browser was used.
- Does the warning appear without SameSite=None?
- Not in the test. Sign-ins of the same app with default cookie options printed no such warning; only the run with SameSite set to None did.
More decoded errors in the Fixes category. For HTTPS on a development machine, see the HTTPS developer certificate.