ADR: Identity and the session token, explained for a new developer
- Status
- accepted
- Date
- 2026-09-03
The Operating Contract asks for a
junior-developer record for any foundational configuration this project
touches, and ADR: Accounts and per-user bids touched the composition root, the
request pipeline and the database schema in one change. That record says what
was decided. This one says what the words mean.
Read it beside api/TheYard.Api/Program.cs and api/TheYard.Api/Shared/Auth/Tokens.cs.
The samples below are those files, read from this build.
In plain words
This page explains, for a developer new to ASP.NET Core, how the site knows who is calling it. Accounts and password hashes live in a library (ASP.NET Core Identity), and a signed proof of who you are (a token) travels in a cookie the page's scripts cannot read.
What that is worth: a developer can add a protected endpoint or a user field by following the page's own checklist, and the organization's sign-in code can be maintained by its newest hire, with the signing key kept out of the repository.
Context
Before this change the API had no idea who was calling it. There was one set of
bids in memory, and "you are the high bidder" meant "the only person in the
room is". Adding accounts meant answering four questions that a newcomer to
ASP.NET Core will meet in that order:
- Where do usernames and password hashes live, and who writes that code?
- Once somebody proves who they are, how does the next request know?
- How does the server read that proof back without trusting the browser?
- How does an endpoint say "not for strangers"?
The answers are Identity, a signed token, a cookie the page cannot read, and
RequireAuthorization(). Each gets a section.
1. What Identity is
ASP.NET Core Identity is two things wearing one name.
The first is a schema: seven tables, all prefixed AspNet, holding users,
their password hashes, their roles, their external logins and their claims. You
can see them created in
api/TheYard.Infrastructure/Migrations/20260903084932_AccountsAndPerUserBids.cs.
This project uses one of the seven. The other six are along for the ride
because the store that manages the first one knows how to manage all of them.
The second is a service, UserManager<YardUser>, which is the only thing
the endpoints actually call. It hashes passwords with PBKDF2 and a per-user
salt, it normalises email addresses so that Steve@Example.com and
steve@example.com cannot both register, and it enforces the password rules
configured beside it. None of that is hard to describe and all of it is easy to
get subtly wrong, which is the argument for not writing it here.
YardUser is the project's own user row. It inherits everything Identity needs
and adds one column:
public sealed class YardUser : IdentityUser
{
public long CreatedAtMs { get; set; }
}
That is the whole customisation. Inheriting from IdentityUser rather than
copying its fields is what lets AddEntityFrameworkStores<YardDbContext> find
the columns it expects.
AddIdentityCore versus AddIdentity
This distinction traps people, so here it is plainly.
AddIdentity<TUser, TRole>() registers the user store and Identity's own
cookie authentication scheme, its sign-in manager, its redirect-to-login-page
behaviour, and a set of cookies named .AspNetCore.Identity.Application. It is
built for a server-rendered site with login pages.
AddIdentityCore<TUser>() registers the user store and stops. No
authentication scheme, no redirects, no second cookie.
This API is a JSON service behind a React page, and it already has an
authentication scheme, described in section 3. Calling AddIdentity here would
install a second, competing one, and the symptom would be a 302 redirect to a
login page that does not exist where a 401 was expected. AddIdentityCore is
the one line that avoids all of that.
services.AddSingleton(new RegistrationLimit(
configuration.GetValue("Accounts:RegistrationsPerHour", RegistrationLimit.DefaultPerHour),
() => DateTimeOffset.UtcNow));
services
.AddIdentityCore<YardUser>(options =>
{
options.Password.RequiredLength = 8;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequireDigit = false;
options.User.RequireUniqueEmail = true;
options.Lockout.MaxFailedAccessAttempts = 5;
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
options.Lockout.AllowedForNewUsers = true;
});
services.AddScoped<IUserStore<YardUser>>(services =>
services.GetKeyedService<IUserStore<YardUser>>(services.GetRequiredService<CurrentBackend>().Key)
?? throw new InvalidOperationException("this request's store keeps no accounts; RequestAccounts answers null before this is reached"));
services.AddScoped(services => new RequestAccounts(() =>
services.GetRequiredService<CurrentBackend>().Backend.Ready
? services.GetRequiredService<UserManager<YardUser>>()
: null));
services.AddScoped(provider => provider.GetRequiredService<RequestAccounts>().Users is { } users
? new AccountsOnThisStore(new IdentityAccountStore(users))
: new AccountsOnThisStore(null));
services.AddSingleton<IResetTokens>(provider => new TokenResets(provider.GetRequiredService<TokenIssuer>()));
services.AddSingleton(provider => new ResetLinkMaker(provider.GetRequiredService<IResetTokens>(), provider.GetRequiredService<IResetLinks>()));
services.AddScoped(provider => new RegisterHandler(Store(provider), provider.GetRequiredService<RegistrationLimit>(), () => Clocks.UtcNow().ToUnixTimeMilliseconds()));
services.AddScoped(provider => new SignInHandler(Store(provider)));
services.AddScoped(provider => new SignOutHandler(Store(provider)));
services.AddScoped(provider => new WhoAmIHandler(Store(provider)));
services.AddScoped(provider => new OperatorResetLinkHandler(Store(provider), provider.GetRequiredService<ResetLinkMaker>()));
services.AddScoped(provider => new ForgotHandler(Store(provider), provider.GetRequiredService<IEmailSender>(), provider.GetRequiredService<ForgotLimit>(), provider.GetRequiredService<ResetLinkMaker>()));
services.AddScoped(provider => new ResetPasswordHandler(Store(provider), provider.GetRequiredService<IResetTokens>(), provider.GetRequiredService<IResetLinks>()));
services.AddScoped<RefuseSession>();
services.AddScoped<SessionEvents>();
services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
options.EventsType = typeof(SessionEvents));
services.AddOptions<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme)
.Configure<TokenIssuer>((options, tokens) => options.TokenValidationParameters = tokens.Validation);
services.AddAuthorization();
Live from api/TheYard.Api/Features/Accounts/Module.cs, region auth, read from this build at 99df7a8.
Why a scoped DbContext sits beside the factory
ADR: The relational store registered AddDbContextFactory<YardDbContext>,
which hands out a fresh context on request rather than registering one.
Everything in this application is a singleton and loads its data once, so a
factory was the right shape.
Identity's Entity Framework store does not use a factory. It asks the container
for a YardDbContext directly, once per request, because that is the pattern
every ASP.NET Core sample uses. So the composition root adds one adapter
between the two:
builder.Services.AddScoped(services =>
services.GetRequiredService<IDbContextFactory<YardDbContext>>().CreateDbContext());
"Scoped" means one instance per HTTP request, disposed when the request ends.
That matters because a DbContext is not thread-safe and holds a change
tracker, so sharing one across requests would let two callers see each other's
half-finished work. This registration is the only scoped one in the
application, and the comment above it in Program.cs says why it exists so
that nobody deletes it as an oddity.
2. What signing a token means
A JWT is three base64 chunks joined by dots: a header saying which algorithm
was used, a payload of claims, and a signature. Two things about it surprise
people.
It is not encrypted. Anyone holding the token can read the claims. Paste
one into any JWT decoder and the email address is right there. That is why
Issue puts the account id and the email in it and nothing else: everything in
the payload is disclosed to whoever gets hold of it, and everything else the
application needs it can look up.
The signature is what makes it trustworthy. HMAC-SHA256 takes the header,
the payload and a secret key, and produces a fixed-length value. Change one
character of the payload and the value no longer matches. Since only the server
knows the key, only the server can produce a signature that validates, so a
token that validates is one this server issued. Forging one means guessing 32
random bytes.
public string Issue(string userId, string email, string store, string? securityStamp) => Issue(userId, email, store, securityStamp, _lifetime);
public string Issue(string userId, string email, string store, string? securityStamp, TimeSpan lifetime) =>
IssueWithFingerprint(userId, email, store, Fingerprint(securityStamp), lifetime);
public string IssueWithFingerprint(string userId, string email, string store, string stampFingerprint, TimeSpan lifetime)
{
return Handler.CreateToken(new SecurityTokenDescriptor
{
Issuer = Issuer,
Audience = Audience,
Subject = new ClaimsIdentity(
[
new Claim(ClaimTypes.NameIdentifier, userId),
new Claim(ClaimTypes.Name, email),
new Claim(StoreClaim, store),
new Claim(StampClaim, stampFingerprint),
]),
Expires = DateTime.UtcNow.Add(lifetime),
SigningCredentials = _credentials,
});
}
private const string ResetAudience = "theyard-reset";
public static readonly TimeSpan ResetLifetime = TimeSpan.FromHours(1);
public string IssueReset(string userId, string store, string? passwordHash)
{
return Handler.CreateToken(new SecurityTokenDescriptor
{
Issuer = Issuer,
Audience = ResetAudience,
Subject = new ClaimsIdentity(
[
new Claim(ClaimTypes.NameIdentifier, userId),
new Claim(StoreClaim, store),
new Claim("fingerprint", Fingerprint(passwordHash)),
]),
Expires = DateTime.UtcNow.Add(ResetLifetime),
SigningCredentials = _credentials,
});
}
public async Task<(string UserId, string Store, string Fingerprint)?> ReadResetAsync(string? token)
{
if (string.IsNullOrWhiteSpace(token))
{
return null;
}
var parameters = Validation.Clone();
parameters.ValidAudience = ResetAudience;
var result = await Handler.ValidateTokenAsync(token, parameters);
if (!result.IsValid)
{
return null;
}
string? id = (result.ClaimsIdentity.FindFirst(ClaimTypes.NameIdentifier) ?? result.ClaimsIdentity.FindFirst("nameid"))?.Value;
string? store = result.ClaimsIdentity.FindFirst(StoreClaim)?.Value;
string? fingerprint = result.ClaimsIdentity.FindFirst("fingerprint")?.Value;
return id is null || store is null || fingerprint is null ? null : (id, store, fingerprint);
}
public static string Fingerprint(string? passwordHash) =>
Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(Encoding.UTF8.GetBytes(passwordHash ?? "")).AsSpan(0, 8));
public static CookieOptions CookieFor(HttpContext context, TimeSpan lifetime) => new()
{
HttpOnly = true,
Secure = context.Request.IsHttps,
SameSite = SameSiteMode.Lax,
Path = "/",
MaxAge = lifetime,
};
Live from api/TheYard.Api/Shared/Auth/Tokens.cs, region issue, read from this build at 99df7a8.
ClockSkew deserves a note. The library defaults to five minutes of slack on
expiry, which is a sensible allowance when two different servers with two
different clocks are involved. Here one process both issues and validates the
token, so five minutes of extra life is slack with no purpose behind it. Thirty
seconds is the setting.
Where the key comes from
Auth:SigningKey is configuration: an environment variable in the container, a
user secret locally, a secret store in a real deployment. When it is absent the
process generates 32 random bytes and logs that it did.
That behaviour is deliberate and its cost is stated out loud: a restart with no
configured key signs everybody out, because the new process cannot validate
tokens the old one signed. The alternative a repository must never take is a
committed default. A signing key in source control is every session forever for
anyone who can read the repository, and it stays true after the file is deleted
because git keeps history.
3. Why the cookie is httpOnly
The token proves who you are. Whoever holds it is you, which is the property
that makes bearer tokens convenient and also the one that makes them dangerous.
The common alternative is localStorage: the page keeps the token in a
variable and sets an Authorization header on each request. That works, and it
means every script running on the page can read the token. Any injected script,
any compromised dependency, any browser extension with page access can take it
and use it from anywhere.
HttpOnly = true tells the browser to withhold the cookie from JavaScript
entirely. document.cookie does not show it. The page never has the token, so
the page cannot leak it, and the client code gets shorter rather than longer:
src/api/auth.ts never touches a token at all.
The other attributes each earn their place:
SameSite = Lax stops the cookie from riding along on a cross-site POST,
which is the shape a CSRF attack takes. Lax rather than Strict so that
arriving from an external link still finds you signed in.
Secure tells the browser to send the cookie over HTTPS only. It is
computed rather than hardcoded, because behind the edge this process is
spoken to over plain HTTP even when the visitor's connection was HTTPS the
whole way. X-Forwarded-Proto is the header that carries that fact across
the hop, and ADR: Edge economics explains the hop.
Path = "/" so one cookie covers the API and the page. Deleting a cookie
means re-sending it with the same path and same-site attributes, which is why
the logout endpoint calls the same CookieFor helper the login endpoint
does. Get that wrong and the browser keeps a second cookie of the same name
on a different path, and the user stays signed in after signing out.
4. How the server reads it back
AddJwtBearer expects the token in an Authorization: Bearer ... header. This
project puts it in a cookie, so one event hook redirects the lookup:
options.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
if (context.Request.Cookies.TryGetValue(TokenIssuer.CookieName, out string? cookie))
{
context.Token = cookie;
}
return Task.CompletedTask;
},
};
OnMessageReceived runs before validation and its job is to answer "where is
the token". Setting context.Token says: here, use this. Everything after that
point is the normal path, signature check included, so nothing about validation
has been weakened by moving where the token was found.
Two lines in the pipeline finish the job:
app.UseAuthentication();
app.UseAuthorization();
Order matters and the names are easy to blur. Authentication is "who are
you": it reads the token and builds HttpContext.User. Authorization is
"may you": it checks that principal against whatever the endpoint requires.
Authentication has to run first, because authorization cannot check a principal
nobody built yet. Put these two after the endpoints and every request arrives
anonymous.
Both run on every request, including the ones that do not require an account.
That is on purpose. A signed-in visitor loading the listing gets a populated
HttpContext.User, which is how GET /api/bids knows whose badges to draw
without a separate request.
5. What RequireAuthorization does
.RequireAuthorization() attaches metadata to one endpoint saying a principal
is required. The authorization middleware reads that metadata and, finding no
authenticated user, ends the request with 401 before the handler runs.
Four endpoints carry it: placing a bid, buying now, clearing your bids, and the
bid history. Reading stays open, so an anonymous visitor still watches the
auction and cannot bid in it.
app.MapPost("/api/auth/register", Register)
.WithName("Register")
.WithTags("Accounts")
.WithSummary("Create an account on this store and sign in")
.WithDescription("Answers the account and sets the session cookie. A few dozen registrations an hour are allowed "
+ "across all strangers, and a refused password gives the slot back.")
.ProducesProblem(StatusCodes.Status400BadRequest)
.ProducesProblem(StatusCodes.Status429TooManyRequests)
.ProducesProblem(StatusCodes.Status503ServiceUnavailable);
app.MapPost("/api/auth/login", Login)
.WithName("Login")
.WithTags("Accounts")
.WithSummary("Sign in and receive the session cookie")
.WithDescription("One sentence for every way this can fail, including a locked account, so the reply never says "
+ "which addresses are registered here. Five wrong passwords lock an account for five minutes.")
.ProducesProblem(StatusCodes.Status400BadRequest)
.ProducesProblem(StatusCodes.Status401Unauthorized)
.ProducesProblem(StatusCodes.Status503ServiceUnavailable);
app.MapPost("/api/admin/reset-links", OperatorResetLink)
.WithName("OperatorResetLink")
.WithTags("Accounts")
.Produces(StatusCodes.Status200OK)
.ProducesProblem(StatusCodes.Status404NotFound)
.ProducesProblem(StatusCodes.Status503ServiceUnavailable);
app.MapPost("/api/auth/forgot", Forgot)
.WithName("ForgotPassword")
.WithTags("Accounts")
.WithSummary("Email a reset link to an address")
.WithDescription("One sentence back whether or not the address has an account here, and one email per address "
+ "per five minutes. A container with no sender configured says so.")
.ProducesProblem(StatusCodes.Status400BadRequest)
.ProducesProblem(StatusCodes.Status503ServiceUnavailable);
app.MapPost("/api/auth/reset", Reset)
.WithName("ResetPassword")
.WithTags("Accounts")
.WithSummary("Use a reset link to choose a new password and sign in")
.WithDescription("The link works once, for an hour, on the site it was minted for. A refused password leaves the "
+ "old one in place.")
.ProducesProblem(StatusCodes.Status400BadRequest)
.ProducesProblem(StatusCodes.Status503ServiceUnavailable);
app.MapPost("/api/auth/logout", Logout)
.WithName("Logout")
.WithTags("Accounts")
.WithSummary("Sign out")
.WithDescription("Deletes the session cookie and answers the anonymous account. Never fails.");
app.MapGet("/api/auth/me", WhoAmI)
.WithName("WhoAmI")
.WithTags("Accounts")
.WithSummary("Who the session belongs to on this store")
.WithDescription("Signed out is an answer, not a 401: a session opened on the other store reads as signed out here.");
Live from api/TheYard.Api/Features/Accounts/AccountEndpoints.cs, region auth-endpoints, read from this build at 99df7a8.
Inside an endpoint that requires it, http.UserId() reads the account id from
the validated claims:
public static string UserId(this HttpContext context) =>
context.User.FindFirstValue(ClaimTypes.NameIdentifier)
?? throw new InvalidOperationException("An authorized request carried no account id");
public static string? UserIdOrNull(this HttpContext context) =>
context.User.FindFirstValue(ClaimTypes.NameIdentifier);
Live from api/TheYard.Api/Shared/Auth/Tokens.cs, region who, read from this build at 99df7a8.
It throws rather than returning null. Reaching that line with no id would mean
authorization admitted a token with no subject, and a graceful fallback there
hides a broken pipeline behind a working-looking page. UserIdOrNull() is the
sibling for endpoints where signing in is optional.
What to change when
- A new endpoint that changes something:
.RequireAuthorization() on the
map call, HttpContext in the parameter list, http.UserId() for the owner.
A test in api/TheYard.Tests/AuthTests.cs should assert it answers 401 to a
caller with no cookie, because that is the assertion that fails when somebody
copies the endpoint above it and forgets the attribute.
- A new field on the user: add the property to
YardUser, then
dotnet ef migrations add <Name> --project api/TheYard.Infrastructure --startup-project api/TheYard.Api. The startup project argument is required
and ADR: The relational store explains why.
- A password rule: the options block inside
AddIdentityCore. Length is
the requirement with evidence behind it; the composition rules mostly teach
people to write the password down, and NIST 800-63B says so at more length.
- The token's contents:
TokenIssuer.Issue. Remember that every claim is
readable by anyone holding the token and is sent on every request.
- A real deployment: set
Auth__SigningKey in the container's environment
from a secret store. Nothing else changes, and nothing about the key ever
enters the repository.
Where it sits
This walkthrough covers host Api code, AddIdentityCore in Composition/AuthRegistration.cs and TokenIssuer in Tokens.cs, plus YardUser and the Identity tables in Infrastructure. Choosing AddIdentityCore over AddIdentity keeps the host small: the JSON API takes the user store it needs and leaves out a cookie scheme and login redirects it would have to fight. A missing Auth:SigningKey costs a sign-out for everyone on restart, which the deploy now avoids by passing one key to both sites. A system that had to revoke a session mid-flight would choose server-side sessions over a self-contained token.
Addendum, 2026-10-03: a second scoped registration
This registration is no longer the only scoped one. RequestAccounts (api/TheYard.Api/Features/Accounts/RequestAccounts.cs) is registered per request beside it in Composition/AuthRegistration.cs, so the account handlers take it as a parameter instead of reaching into the service container. It asks whether the request's store came up before it opens UserManager, which means no handler ever meets the throw in the user store factory.
Enforced by
Not machine-checked. Held by review.