Fix insecure randomness for reset tokens in C# and ASP.NET Core
===============================================================

Generate the token with RandomNumberGenerator.GetBytes(32) (256 bits),
Base64Url-encoded. Never use System.Random -- new Random(seed) is fully
determined by the seed, and a clock-derived seed is tiny -- and never 'fix' a
weak token by hashing it. Compare the stored token in constant time with
CryptographicOperations.FixedTimeEquals, and keep it single-use with a short
expiry.

Page: https://softwaresecurity.com/fix/insecure-randomness/csharp-aspnetcore/

What is in this archive
-----------------------
insecure-randomness/csharp-aspnetcore/   the project: three implementations of the same code and one test suite
  src/Resets/impl/VulnerableTokens.cs   (vulnerable)
  src/Resets/impl/FixedTokens.cs   (fixed)
  src/Resets/impl/WrongTokens.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). run-all.mjs needs Node.js 22 or
  later. Supported versions: https://softwaresecurity.com/fix/versions/

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

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

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

    # every test passes
    IMPL=fixed dotnet test --project tests/Resets.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/Resets.Tests --no-restore --results-directory reports --report-xunit-junit --report-xunit-junit-filename junit.xml
    # a test fails
    IMPL=wrong dotnet test --project tests/Resets.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 insecure-randomness/csharp-aspnetcore
    node run-all.mjs insecure-randomness/csharp-aspnetcore

Verified
--------
  11 October 2026, with dotnetSdk 10.0.112, dotnetRuntime 10.0.12,
  aspnetcoreRuntime 10.0.12. 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.
