Fix session fixation and session cookies in C# and ASP.NET Core
===============================================================

Keep the signed-in user in an authentication cookie, not in ISession, which
cannot change its ID: SignInAsync issues a new ticket at sign-in. Set
Cookie.Name = "__Host-auth", SecurePolicy = Always, HttpOnly and SameSite =
Lax, use ExpireTimeSpan = 30 minutes with SlidingExpiration, and reject
tickets older than 8 hours in OnValidatePrincipal.

Page: https://softwaresecurity.com/fix/session-cookies/csharp-aspnetcore/

What is in this archive
-----------------------
session-cookies/csharp-aspnetcore/   the project: three implementations of the same code and one test suite
  src/Portal/impl/VulnerableSessions.cs   (vulnerable)
  src/Portal/impl/FixedSessions.cs   (fixed)
  src/Portal/impl/WrongSessions.cs   (a plausible fix that still fails)
run-all.mjs   runs the tests in all three modes and checks the results (Node.js only)
LICENSE-CODE.txt

Run it
------
  Tested with .NET 10.0.12 (SDK 10.0.112, ASP.NET Core runtime 10.0.12).
  run-all.mjs needs Node.js 22 or later. Supported versions:
  https://softwaresecurity.com/fix/versions/

  From the session-cookies/csharp-aspnetcore folder, install the dependencies
  once (this needs network access):

    dotnet restore tests/Portal.Tests --locked-mode

  Then run the tests. IMPL chooses the implementation the tests load:

    # every test passes
    IMPL=fixed dotnet test --project tests/Portal.Tests --no-restore --results-directory reports --report-xunit-junit --report-xunit-junit-filename junit.xml
    # an exploit test fails
    IMPL=vulnerable dotnet test --project tests/Portal.Tests --no-restore --results-directory reports --report-xunit-junit --report-xunit-junit-filename junit.xml
    # a test fails
    IMPL=wrong dotnet test --project tests/Portal.Tests --no-restore --results-directory reports --report-xunit-junit --report-xunit-junit-filename junit.xml

  The JUnit report is written to reports/junit.xml. To run all three modes and
  check the results in one step, run the harness from this top folder (it needs
  only Node.js; --install runs the install step first):

    node run-all.mjs --install session-cookies/csharp-aspnetcore
    node run-all.mjs session-cookies/csharp-aspnetcore

Verified
--------
  11 October 2026 on macOS 27.0 (Darwin 27.0.0, arm64), with dotnetSdk 10.0.112,
  aspNetCoreRuntime 10.0.12, mvcTesting 10.0.12, xunitV3 4.0.1,
  microsoftTestingPlatform 2.4.0. Versions and results hold for that date: run
  the tests with your own versions before relying on the fix.

Licence
-------
  The code is MIT licensed (LICENSE-CODE.txt). The page text is CC BY 4.0.
