Getting started
Password login with two-factor authentication, on SQLite, in one Program.cs. Everything else - an identity provider, passkeys, another database - is a step on from this, linked at the end.
Needs .NET 10.
Quick start
dotnet new web -n MyApp && cd MyApp
dotnet add package Toamaisutaa.AspNetCore
dotnet add package Toamaisutaa.EntityFrameworkCore.Migrations.SqliteTwo keys, one to sign tokens and one to encrypt the two-factor secrets. Generate each:
openssl rand -base64 32
# or, in PowerShell: [Convert]::ToBase64String([Security.Cryptography.RandomNumberGenerator]::GetBytes(32))appsettings.json:
{
"LocalLogin": {
"SigningKey": "<first key>",
"Audience": "my-app"
},
"TwoFactor": {
"EncryptionKey": "<second key>"
}
}appsettings.Development.json - open registration on your machine only, so there is an account to try things with:
{
"LocalLogin": {
"AllowSelfRegistration": true
}
}Program.cs:
using Microsoft.EntityFrameworkCore;
using Toamaisutaa.Abstractions;
using Toamaisutaa.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddToamaisutaaBearer(builder.Configuration);
builder.Services.AddToamaisutaaAuthorization(builder.Configuration);
builder.Services.AddToamaisutaaDbContext(db => db.UseSqlite("Data Source=app.db",
sqlite => sqlite.MigrationsAssembly("Toamaisutaa.EntityFrameworkCore.Migrations.Sqlite")));
builder.Services.AddToamaisutaaPasswordLogin(builder.Configuration);
builder.Services.AddToamaisutaaTwoFactor(builder.Configuration);
// Prints links to the console, which is fine on your machine and nowhere else: anyone who can read
// the logs could take over any account. Startup refuses it outside Development.
if (!builder.Environment.IsDevelopment())
throw new InvalidOperationException("ConsoleMail prints reset and verification links to the console. Register real mail first.");
builder.Services.AddSingleton<ConsoleMail>();
builder.Services.AddSingleton<IPasswordResetNotifier>(services => services.GetRequiredService<ConsoleMail>());
builder.Services.AddSingleton<IEmailVerificationNotifier>(services => services.GetRequiredService<ConsoleMail>());
var app = builder.Build();
using (var scope = app.Services.CreateScope())
scope.ServiceProvider.GetRequiredService<ToamaisutaaDbContext>().Database.Migrate();
app.UseAuthentication();
app.UseAuthorization();
app.MapToamaisutaaPasswordEndpoints();
app.MapToamaisutaaTwoFactorEndpoints();
app.MapGet("/hello", () => "Signed in.");
app.Run();
// Replace with real mail before anyone else uses this.
sealed class ConsoleMail : IPasswordResetNotifier, IEmailVerificationNotifier
{
public Task SendAsync(ToamaisutaaUser user, string resetToken, CancellationToken cancellationToken = default)
{
Console.WriteLine($"Reset token for {user.Email}: {resetToken}");
return Task.CompletedTask;
}
public Task SendAsync(ToamaisutaaUser user, string email, string verificationToken, CancellationToken cancellationToken = default)
{
Console.WriteLine($"Verification token for {email}: {verificationToken}");
return Task.CompletedTask;
}
}dotnet run. Every endpoint now needs a token unless it says .AllowAnonymous(), and a missing key or setting stops startup with a message naming it rather than failing at the first request.
A password reset goes only to an address that has been verified, which is why a verification notifier is registered even this early: without one, startup refuses, because no reset could ever be sent.
Try it
- Register -
POST /auth/registerwith{ "userName": "ada", "email": "[email protected]", "password": "correct horse battery staple" }. Answers201with anaccess_token. Run in Development (dotnet runis, by default), since registration is only open there. - Call your endpoint -
GET /hellowithAuthorization: Bearer <access_token>.200; without the header,401. - Turn on two-factor -
POST /auth/2fa/beginwith{ "currentPassword": "..." }and the token. Scan the returneduri(or type thesecret) into an authenticator app, thenPOST /auth/2fa/confirmwith{ "code": "123456" }. Answers the recovery codes, once. - Sign in -
POST /auth/loginwith{ "identifier": "ada", "password": "..." }now answerstwo_factor_required: trueand achallengeinstead of tokens.POST /auth/2fa/verifywith{ "challenge": "...", "code": "123456" }answers the tokens.
Refresh with POST /auth/refresh and { "refreshToken": "..." }, sign out with POST /auth/logout. Every route and body: local password login.
Before it goes public
- Keys from the environment, never committed:
LocalLogin__SigningKey,TwoFactor__EncryptionKey. The sample's published keys are refused outside Development. Signing local tokens covers rotating keys and asymmetric signing. - Real mail for password resets and address verification: your own notifiers, or the SMTP package - provisioning accounts. The console one above refuses to start outside Development, because anyone reading the logs could take over any account.
- Behind a reverse proxy, forward the client's address, or every caller shares one rate limit - behind a proxy.
- Decide who may register. It is on in Development only. Turned on everywhere, anyone can create an account and call every endpoint that only asks for a signed-in user, and registration answers 409 for a taken name, which tells a stranger who has an account. Without it, accounts come from an admin or an invitation - provisioning accounts.
- A production database - storage and migrations.
Next steps
| To add | Read |
|---|---|
| Sign-in through Keycloak, Entra, Authentik or another OIDC provider | OIDC bearer validation |
Postgres, SQL Server, MySQL, or your own DbContext | Storage and migrations |
| A browser front end: the sign-in branch, refresh, where tokens live | Using this from a SPA |
| Passkeys | Passkeys |
| "Remember this device" for the second factor | Trusted devices |
| Sign-in by emailed link | Magic-link sign-in, which needs email verification |
| Admin-created accounts and invitations | Provisioning accounts |
| A list of a user's sessions, and signing out elsewhere | Sessions |
| Roles for local accounts, and other hooks | Customizing local login |
| Who is calling, from a service | The current user |
| Argon2id, or a breached-password check | Password hashing, breached passwords |
| An audit trail, or metrics | Audit events, metrics |
| These endpoints in your OpenAPI document | OpenAPI |
With an identity provider instead
A resource server whose users all come from an OIDC provider needs no database and no keys of its own:
dotnet add package Toamaisutaa.AspNetCorebuilder.Services.AddToamaisutaaBearer(builder.Configuration);
builder.Services.AddToamaisutaaAuthorization(builder.Configuration);
app.UseAuthentication();
app.UseAuthorization();{
"Oidc": {
"Authority": "https://id.example.com",
"ClientId": "your-app",
"RoleClaim": "roles",
"AdminRole": "admin"
}
}Both together work too: local password login next to the provider, each validated by the same pipeline. OIDC bearer validation has every key.
The sample
samples/MinimalApiSample in the repository wires nearly everything at once against a throwaway identity provider: password login, two-factor, passkeys, trusted devices, magic links, sessions, OpenAPI. Four lines of Docker and a dotnet run.