Local password login
The fallback, for deployments that cannot run an identity provider. Switching it on means becoming the identity provider - storing credentials, deciding who is who, issuing something the client presents afterwards. Use OIDC if you can.
builder.Services.AddToamaisutaaPasswordLogin(builder.Configuration); // section "LocalLogin"
builder.Services.AddSingleton<IPasswordResetNotifier, YourEmailSender>();
app.MapToamaisutaaPasswordEndpoints();Writing the browser side of this? Using this from a SPA covers the whole flow in one place - the sign-in branch, refresh on 401, and where the tokens should live.
It needs AddToamaisutaaBearer as well - the tokens it issues are validated by the same pipeline that validates your identity provider's, so policies, ICurrentUser and provisioning cannot tell the two apart. A store registration and an IPasswordResetNotifier are also required, and all three are checked at startup rather than at the first request.
A user may have a password, external logins, or both. Adding a password to an account that arrived through OIDC is supported and touches nothing on the external side.
The endpoints
| Method | Route | Answers |
|---|---|---|
| POST | /auth/login | 200 with a token pair, 200 with a two-factor challenge, 200 with an email-verification challenge, or 401 |
| POST | /auth/refresh | 200 with a rotated pair, or 401 |
| POST | /auth/logout | 204 |
| POST | /auth/register | 201 with a token pair or an email-verification challenge, 400, or 409. Only mapped when AllowSelfRegistration is true |
| GET | /auth/password | 200 with { "has_password": true } or false. Authenticated |
| POST | /auth/password | 204, 400, or 429. Authenticated. Sets a first password or changes an existing one |
| POST | /auth/password/forgot | 204, always |
| POST | /auth/password/reset | 204 or 400 |
| POST | /auth/email | 204, 400, 409, or 429. Authenticated. Only mapped when an IEmailVerificationNotifier is registered |
| POST | /auth/email/verify | 204, 400, or 409. Only mapped when an IEmailVerificationNotifier is registered |
| POST | /auth/email/verify-code | 200 with a token pair, 200 with a two-factor challenge, 400, or 401. Only mapped when RequireVerifiedEmailForSignIn is true |
| POST | /auth/email/resend-code | 204, 401, 429, or 502. Only mapped when RequireVerifiedEmailForSignIn is true |
| POST | /auth/email/change-address | 200 with a new challenge, 400, 401, or 409. Only mapped when RequireVerifiedEmailForSignIn is true |
| POST | /auth/magic-link | 204, always. Only mapped when an IMagicLinkNotifier is registered |
| POST | /auth/magic-link/verify | 200 with a token pair, 200 with a two-factor challenge, or 401. Only mapped when an IMagicLinkNotifier is registered |
| POST | /auth/users | 201, 400, 403, or 409. Admin only. Only mapped when an IAdminPasswordIssuedNotifier is registered and Oidc:AdminRole is set |
| POST | /auth/users/{userId}/password | 204, 400, 403, or 502. Admin only. Only mapped when an IAdminPasswordIssuedNotifier is registered and Oidc:AdminRole is set |
| POST | /auth/invitations | 201, 400, 403, or 502. Admin only. Only mapped when an IInvitationNotifier is registered and Oidc:AdminRole is set |
| POST | /auth/invitations/revoke | 204, 403, or 404 when the address has no open invitation. Admin only. Mapped with /auth/invitations |
| POST | /auth/invitations/complete | 201 with a token pair, 400, or 409. Only mapped when an IInvitationNotifier is registered |
| GET | /auth/.well-known/jwks.json | 200 with the public signing keys. Only mapped when SigningKeys is set - see signing local tokens |
The session list (/auth/sessions) is mapped separately, by MapToamaisutaaSessionEndpoints() - see sessions.
Requests are camelCase, token responses are not
Request bodies bind to this package's own records, so they are camelCase: identifier, refreshToken, newPassword. Sign-in responses use the RFC 6749 names - access_token, refresh_token, expires_in, token_type - because a token endpoint is a place where a standard already exists. Everything else this package returns is camelCase.
Bodies
POST /auth/login - deviceToken is optional, and only meaningful with trusted devices.
{ "identifier": "ada", "password": "correct horse battery staple", "deviceToken": null }Answers 200 with a token pair: a short-lived access token, signed locally, and an opaque refresh token. recovery_codes_running_low is true or null, never false; device_token and device_expires_in are null unless a device was trusted.
{
"access_token": "eyJhbGciOiJIUzI1NiIs...",
"refresh_token": "VWv8Paxg53FWF4HQ_Xzwp8o1EI3YWLSV4PbZAoH1x2M",
"expires_in": 900,
"token_type": "Bearer",
"recovery_codes_running_low": null,
"device_token": null,
"device_expires_in": null
}Or 200 with a challenge and no tokens, for a user who has enrolled in two-factor authentication. Present it with a code to /auth/2fa/verify to finish signing in.
{ "two_factor_required": true, "challenge": "No1CXq9-...", "expires_in": 300, "second_factors": ["otp"] }Branch on two_factor_required
Both shapes are 200, and the second does not appear until a user enrols - after which it appears for them every time. A client that checks response.ok and reads access_token works until the first person turns on two-factor, then breaks for that person only. Every endpoint that ends a sign-in - /auth/login, /auth/magic-link/verify, /auth/passkeys/assertion/complete - can answer either shape.
POST /auth/refresh answers the same token-pair shape, or 401.
{ "refreshToken": "VWv8Paxg53FWF4HQ_Xzwp8o1EI3YWLSV4PbZAoH1x2M" }POST /auth/logout answers 204 whether or not that token existed.
{ "refreshToken": "VWv8Paxg53FWF4HQ_Xzwp8o1EI3YWLSV4PbZAoH1x2M" }POST /auth/register answers 201 with the same token-pair shape, so a registration signs the user straight in - unless LocalLogin:RequireVerifiedEmailForSignIn is on, when it answers 201 with an email_verification_required challenge and mails a code. See requiring a verified address to sign in.
{ "userName": "ada", "email": "[email protected]", "password": "correct horse battery staple" }GET /auth/password - authenticated. { "has_password": false } for an account that arrived through an identity provider and has never set one, so a client knows whether to ask for the current password or offer a first one.
POST /auth/password - authenticated. Omit currentPassword when the account arrived through an identity provider and is gaining its first password. Answers 204, 400, or 429 when the per-address rate limit refuses it.
A first password has no current one to prove, so it needs a recent sign-in instead: the caller's token must carry an auth_time from the identity provider, or a toa_2fa_at, or belong to a session (toa_sid) that started, within LocalLogin:FirstPasswordProofWindow (five minutes by default). A local token issued after an identity-provider sign-in carries neither claim, so the session's start is what counts there; a refresh keeps that start, so it never counts as a new sign-in. Otherwise a stolen access token would be enough to add a password that outlives it. Send the user back through the identity provider with max_age or prompt=login first, so the token they come back with is fresh.
The access token has to carry auth_time
It is read off the token presented here, which is the access token, and many providers put auth_time only in the ID token. Entra and Auth0 leave it out of access tokens by default, and prompt=login does not change that. Configure the provider to include it - an optional or custom claim on the access token - or these accounts will get 400 however recently they signed in. Keycloak includes it by default.
The new credential signs in with the user name and carries no email address, because the provider's email was never proven to this package. Add the address through /auth/email, which proves it.
The user name is the provider's handle, and never its email. An account whose handle is missing or shaped like an address (a UPN is one) gets 400 here, and can use passkeys or magic links instead.
A user name cannot contain @, wherever it is chosen - registration, admin creation, invitation completion - and answers 400. The sign-in box takes a user name or an email, so an address-shaped user name would claim that address for sign-in before its owner arrived.
{ "currentPassword": "the old one", "newPassword": "the new one" }POST /auth/password/forgot answers 204 always - for an unknown address and for an account an identity provider owns alike. The lookup and the mail happen after the response, on a background queue, so a real account takes no longer to answer than an unknown address. A second request for the same address inside LocalLogin:MailRequestCooldown (a minute by default) is dropped, whether or not the address has an account.
Your notifier runs without a request
IPasswordResetNotifier.SendAsync is called after the 204, in a scope of its own, with no HttpContext. A notifier that builds its link from IHttpContextAccessor or the request's host throws there, the failure is only logged, and reset mail stops arriving. Build the link from configuration.
{ "email": "[email protected]" }POST /auth/password/reset answers 204 or 400.
{ "token": "the token from the notifier", "newPassword": "the new one" }POST /auth/email - authenticated, mails a verification link to newEmail and moves nothing until it is redeemed. currentPassword is required. See Email verification.
{ "newEmail": "[email protected]", "currentPassword": "the one they signed in with" }POST /auth/email/verify - anonymous, but only usable with a valid token. Writes the address the token names and stamps it confirmed.
{ "token": "the token from the notifier" }POST /auth/users - admin only, provisions an account on someone else's behalf. password is optional; omit it and Toamaisutaa generates one. Never signs the caller in as the new account, and the response never carries a password - see Admin-provisioned accounts.
{ "userName": "newteacher", "email": "[email protected]", "password": null }{ "userId": "0199...", "userName": "newteacher", "email": "[email protected]" }POST /auth/users/{userId}/password - admin only, overwrites userId's password with no current-password check. password is optional; omit it and Toamaisutaa generates one. 502 means the password was set but could not be delivered; no answer ever carries a password.
The password is mailed only to the credential's own address, never to the profile email an identity provider writes. An account with none gets no mail, so give the password in the request and deliver it another way; leaving it out there answers 400 before anything changes, because a generated password would reach nobody. Creating a user with neither an email nor a password is refused the same way. With LocalLogin:RequireVerifiedEmailForPasswordReset on, an address that was never verified answers 400 before anything changes.
{ "password": null }POST /auth/invitations - admin only, reserves an account with nothing but an email. 502 means nothing could be delivered and the reservation was rolled back. Never returns the invitation token - see Completing a reserved invitation.
{ "email": "[email protected]" }{ "userId": "0199...", "email": "[email protected]" }POST /auth/invitations/complete - anonymous, but only usable with a valid token. Sets the user name and password on the one reserved account the token names, and signs in - the same shape /auth/register answers with. 409 is a taken user name.
{ "token": "the token from the notifier", "userName": "newparent", "password": "the one they chose" }The two error shapes
A credential that was not accepted answers 401 with the RFC 6749 shape. Wrong password, no such account, locked out and an unknown refresh token are all this one body, because telling them apart tells a caller which user names are real:
{ "error": "invalid_grant", "error_description": "The credentials are not valid." }Input the caller can correct answers 400 - or 409 for a taken user name - with a camelCase array. These strings are written to be shown to the person who typed the input:
{ "errors": ["Use at least 8 characters."] }Things to know before switching it on
Password hashing, and how to replace it, is its own page: Password hashing. So are the other three swap-this-interface seams - the password rules, what goes in the access token, and roles: Customizing local password login.
Lockout locks out the guesser, not the owner
Wrong passwords at sign-in are counted per account and per source. Five from one source in fifteen minutes lock that source out of that account for fifteen minutes; the owner signing in from anywhere else is not affected. A source is an IPv4 address, or an IPv6 /64.
Guessing spread across many sources is stopped by a ceiling: fifty wrong passwords from every source together lock the account for everyone. Both numbers are settings, MaxFailedAttempts and MaxFailedAttemptsPerAccount.
A trusted device is its own source. A caller presenting a valid device token is counted against that device rather than its address, and is not held to the ceiling. That is what keeps an owner signing in when somebody on the same address - a school or an office behind one public IP - has locked it, or when the ceiling has been reached. A stolen device token gets the same five guesses as any one address, not unlimited ones.
A right password clears its own source's count, and gives back only its own attempt on the ceiling, so the owner signing in does not wipe out guesses made from elsewhere. Setting a new password - a reset, a change, or an administrator's - clears all of them.
Wrong second-factor codes and wrong current passwords are different: they need the password or a session already, so they are counted against the account itself, and five of those lock it for everyone. See two-factor lockout.
Per-IP rate limiting on the anonymous endpoints covers the rest, and is enforced by the endpoints themselves rather than by middleware you have to remember to add.
Behind a proxy, configure forwarded headers
The limiter keys on the connection's address. An IPv4 caller counts as its address. An IPv6 caller counts as its /64, and all the /64s of one /48 also share Ipv6SitePermitLimit between them. Behind a reverse proxy the connection comes from the proxy, so unless UseForwardedHeaders() rewrites it, every client shares one limit and ten junk logins a minute answer 429 for the whole site. When requests arrive from a private or loopback address with X-Forwarded-For still on them, the package logs a warning once saying so.
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
options.KnownProxies.Add(IPAddress.Parse("10.0.0.2")); // your proxy, and only your proxy
});
app.UseForwardedHeaders();Many people behind one address
A school or an office sends everyone out through one public address, and one person there sending junk logins would otherwise spend the budget for all of them. Two things keep that contained:
- A signed-in caller spends a budget of its own, keyed on its subject, on every endpoint that requires the sign-in - the session list, two-factor management, passkeys. A token sent to an anonymous endpoint such as
/auth/loginbuys nothing, or one throwaway account would be a fresh budget for guessing. LocalLogin:RateLimit:SharedNetworksnames the networks you know are shared, in CIDR form. Each spendsSharedNetworkPermitLimit(300 per window by default) instead of its address's ten.
{
"LocalLogin": {
"RateLimit": {
"SharedNetworks": [ "203.0.113.7/32" ],
"SharedNetworkPermitLimit": 300
}
}
}There is no separate limit per user name: wrong passwords against one account are already capped by lockout's account-wide ceiling.
The budgets live in each process's memory. Several instances behind one balancer each grant the full limit, so put a shared limiter in front - at the gateway or the balancer - if that matters.
Registration reveals whether an account exists
A taken user name answers 409. Hiding that needs an email round trip, and email delivery is deliberately not in this package. Registration is off by default; turning it on accepts this.
An address registration takes is held unproven. Whoever later proves it - by redeeming an /auth/email verification link, or by completing an invitation sent to it - takes it over, and the account that only typed it loses it. Otherwise registering somebody else's address first would lock its owner out of every way in.
For the same reason a local access token carries email only once the address is verified, and then it is the credential's address, read again on every issue including a refresh. Everything downstream reads email as proven. If you replace IAccessTokenIssuer, write AccessTokenRequest.VerifiedEmail, not User.Email.
That email round trip - and two other ways to get someone into an account without open registration - are their own page: Provisioning accounts. Proving that an address belongs to whoever typed it, and changing it afterwards, is Email verification.
Revoking sessions means local sessions
A password change or reset revokes every refresh token this package issued. An access token your identity provider issued keeps working until it expires, because we cannot revoke it.
A local access token carries the account's security stamp in toa_stamp, and every credential change moves the stamp. Where the stamp is compared, the old token stops working at once:
- Endpoints carrying the admin policy always compare it, so a stolen admin token stops working the moment the owner changes their password.
- Every other endpoint compares it only with
LocalLogin:CheckSecurityStampOnEveryRequeston. That costs one primary-key read per request. With it off, a local token keeps working for up toAccessTokenLifetimeon an endpoint that does not resolveICurrentUser.
Either way a refused token is a 401 with error="invalid_token", so the client refreshes and the refresh is refused too.
Removing a role does not move the stamp, because roles come from your own IUserRoleProvider. Call AdminRevokeSessionsAsync (or POST /auth/users/{userId}/sessions/revoke) after taking a role away.
It also un-trusts every device and deletes every passkey on the account. Both are ways of signing in that a new password would otherwise not touch, and somebody changing their password is usually reacting to exactly that.
Expired tokens accumulate unless you sweep them
AddToamaisutaaTokenCleanup() runs a periodic delete over every expiring row this package writes - refresh tokens, reset tokens, invitation tokens, email verification tokens, magic-link tokens, spent sign-in throttle rows, and the two-factor challenge and trusted-device rows when those are configured. Without it, plan to call DeleteExpiredAsync on each of those stores from your own scheduler.
Refresh rows are the exception to "once expired". A rotated row is what reuse detection works from, so the sweep keeps it until its family is past RefreshTokenAbsoluteLifetime, not merely past its own expiry. If you schedule the refresh sweep yourself, pass now - (RefreshTokenAbsoluteLifetime - RefreshTokenLifetime) rather than now, or a stolen token replayed after two weeks is answered as unknown instead of revoking the session it was stolen from.
Refresh tokens
Stored hashed, never in the clear, and rotated on every use. Presenting a token that has already been rotated proves two parties hold the chain, so the whole family is revoked and the event is logged loudly - the standard mitigation for a stolen refresh token.
Rotation alone would keep a session alive forever, so a chain also has an absolute lifetime (RefreshTokenAbsoluteLifetime, 90 days) measured from the sign-in that started it.
A family is what this package means by a session, and a user can see and end their own: Sessions.
Calling the services yourself
The endpoints are a convenience, not the API. IPasswordSignInService and IPasswordAccountService are public, so an application that routes everything through its own handlers - a mediator, a different transport, a background job - can skip MapToamaisutaaPasswordEndpoints entirely and call them directly.
public sealed class SignInHandler(IPasswordSignInService signIn)
{
public async Task<YourResult> Handle(YourCommand command, CancellationToken cancellationToken)
{
var result = await signIn.SignInAsync(
new PasswordSignInRequest
{
Identifier = command.Identifier,
Password = command.Password,
UserAgent = command.UserAgent, // yours to supply - Core never sees an HTTP request
IpAddress = command.IpAddress,
},
cancellationToken);
return result.Outcome switch
{
SignInOutcome.Succeeded => YourResult.SignedIn(result.Tokens!),
SignInOutcome.TwoFactorRequired => YourResult.NeedsCode(result.Challenge!),
_ => YourResult.Refused(),
};
}
}Two things worth knowing before you build on them:
SignInOutcometells you what really happened, and the endpoints deliberately throw that away.UnknownUser,InvalidPasswordandLockedOutare three different values here and one 401 on the wire. If you shape your own response, keep that collapse.- Every result type is a sealed record with public
initmembers, so they compose into DTOs of your own and construct in a test without reflection.SignInResult.Succeededis computed fromOutcome, so set the outcome rather than looking for a setter.
The implementations behind these interfaces are internal. Inject the interface - which is what DI hands you - rather than expecting to construct one.
Configuration
| Key | Default | Notes |
|---|---|---|
LocalLogin:SigningKey | Base64, at least 32 bytes. HS256. Required unless SigningKeys is set, and there is no generated fallback either way | |
LocalLogin:SigningKeys | empty | Asymmetric keys, active one first, published as JWKS. See signing local tokens |
LocalLogin:Issuer | toamaisutaa | Changing it invalidates every token in flight |
LocalLogin:Audience | Oidc:ClientId | |
LocalLogin:AccessTokenLifetime | 00:15:00 | |
LocalLogin:CheckSecurityStampOnEveryRequest | false | Refuses a local token issued before a credential changed on every request, not only on admin endpoints. One read per request |
LocalLogin:RefreshTokenLifetime | 14.00:00:00 | |
LocalLogin:RefreshTokenAbsoluteLifetime | 90.00:00:00 | How long a rotating chain may live |
LocalLogin:Pbkdf2Iterations | 600000 | Startup floor; 50000000 is the ceiling a stored row may name |
LocalLogin:SaltSizeBytes / HashSizeBytes | 16 / 32 | Startup floor; HashSizeBytes has a ceiling of 1024 |
LocalLogin:Pepper / PepperVersion / RetiredPeppers | none / 1 / empty | See password hashing |
LocalLogin:LockoutEnabled | true | |
LocalLogin:MaxFailedAttempts | 5 | Wrong passwords per source at sign-in; wrong codes and current passwords per account |
LocalLogin:MaxFailedAttemptsPerAccount | 50 | Wrong passwords from every source together before the account locks for everyone |
LocalLogin:LockoutWindow / LockoutDuration | 00:15:00 | |
LocalLogin:LockoutMaxDuration | 1.00:00:00 | What a repeated lock from wrong codes or current passwords doubles up to. Repeated locks |
LocalLogin:SignInRefusalFloor | 00:00:01 | The least time a refused /auth/login takes, so an unknown name, a wrong password and a locked account cannot be told apart by the clock |
LocalLogin:FirstPasswordProofWindow | 00:05:00 | How recent a sign-in must be to give a passwordless account its first password |
LocalLogin:MinimumPasswordLength | 8 | NIST: a length floor, no composition rules |
LocalLogin:MaximumPasswordLength | 128 | Not a strength rule - a bound on an anonymous endpoint. Sign-in refuses a longer password without hashing it, so lowering it cuts off longer passwords set before |
LocalLogin:MaxRequestBodyBytes | 262144 | The largest body any of the package's endpoints reads. Kestrel answers 413 above it, before reading |
LocalLogin:PasswordResetTokenLifetime | 01:00:00 | Single use |
LocalLogin:MailRequestCooldown | 00:01:00 | One reset or magic-link request per address, and one email-change link per account; zero turns it off |
LocalLogin:InvitationTokenLifetime | 7.00:00:00 | Single use |
LocalLogin:EmailVerificationTokenLifetime | 1.00:00:00 | Single use |
LocalLogin:MagicLinkTokenLifetime | 00:15:00 | Single use. See magic-link sign-in |
LocalLogin:RequireVerifiedEmailForPasswordReset | true | See email verification before upgrading |
LocalLogin:RequireVerifiedEmailForSignIn | false | A password sign-in waits on a confirmed address. See email verification |
LocalLogin:EmailVerificationCodeLifetime | 00:15:00 | Single use |
LocalLogin:EmailVerificationChallengeLifetime | 01:00:00 | |
LocalLogin:AllowSelfRegistration | false | When false the endpoint is not mapped at all |
LocalLogin:EndpointPrefix | /auth | |
LocalLogin:SessionEndpointPrefix | /sessions | Appended to EndpointPrefix. See sessions |
LocalLogin:IpAddressStorage | None | What a session row keeps of the caller's address. See sessions |
LocalLogin:RateLimit:Enabled | true | Per caller address, fixed window, one budget across the anonymous endpoints. A signed-in caller on an endpoint requiring it spends its own |
LocalLogin:RateLimit:PermitLimit / Window | 10 / 00:01:00 | |
LocalLogin:RateLimit:Ipv6SitePermitLimit | 100 | Shared by every /64 of one IPv6 /48 per window. Raise it if many unrelated users reach you from one carrier's /48 |
LocalLogin:RateLimit:SharedNetworks:0 | A network many people share one address from, in CIDR form. Shared addresses | |
LocalLogin:RateLimit:SharedNetworkPermitLimit | 300 | What each shared network may spend per window |
LocalLogin:RateLimit:MaxConcurrentChecks | twice the processor count, at least 4 | Password and code checks running at once across the whole process, whatever the callers' budgets. Not switched off by Enabled |
LocalLogin:RateLimit:MaxQueuedChecks | 100 | Checks that wait for a slot; past that, 503 |
LocalLogin:RateLimit:Nat64Prefixes:0 | A network-specific NAT64 prefix in front of you, such as 2001:db8:64::/96. Its IPv4 clients are keyed on their own address rather than all sharing the gateway's. 64:ff9b::/96 and 64:ff9b:1::/48 are handled without it | |
LocalLogin:TokenCleanupInterval | 06:00:00 | Only used by AddToamaisutaaTokenCleanup() |