MindAttic.Authentication/INTEGRATION
Integrating MindAttic.Authentication into an app
A consuming app keeps its own DbContext, connection string, and deployment. It gains hardened auth by (1) referencing the package, (2) implementing IAuthDataContext on its DbContext, (3) wiring DI + middleware + endpoints, (4) adding login/MFA/account pages, and (5) provisioning Vault secrets.
Every consuming app (today MindAttic.Ideas, IdiotProof, Prose and Tutor) is a separate trust boundary — each picks a unique
AppName, so a cookie from one app can never authenticate to another.
1. Reference the package
nuget.config must include the local feed:
<add key="LocalNuGet" value="C:\LocalNuGet" />
<PackageReference Include="MindAttic.Authentication" Version="6.0.0" /> <PackageReference Include="MindAttic.Vault" Version="5.0.0" /> <!-- only if the host fills config through Vault; the library itself does not depend on it -->
2. Implement IAuthDataContext on your DbContext
using MindAttic.Authentication.Data;
using MindAttic.Authentication.Entities;
public sealed class AppDbContext(DbContextOptions<AppDbContext> o) : DbContext(o), IAuthDataContext
{
// ... your app's DbSets ...
public DbSet<AuthUser> AuthUsers => Set<AuthUser>();
public DbSet<AuthUserMfa> AuthUserMfa => Set<AuthUserMfa>();
public DbSet<AuthRecoveryCode> AuthRecoveryCodes => Set<AuthRecoveryCode>();
public DbSet<AuthSession> AuthSessions => Set<AuthSession>();
public DbSet<AuthLoginThrottle> AuthLoginThrottles => Set<AuthLoginThrottle>();
public DbSet<AuthAuditLog> AuthAuditLog => Set<AuthAuditLog>();
public DbSet<AuthPasswordHistory> AuthPasswordHistory => Set<AuthPasswordHistory>();
public DbSet<AuthPasswordResetToken> AuthPasswordResetTokens => Set<AuthPasswordResetToken>();
protected override void OnModelCreating(ModelBuilder b)
{
base.OnModelCreating(b);
// ... your app's config ...
b.ApplyMindAtticAuthConfiguration(); // tables in the isolated `auth` schema
}
}
Then dotnet ef migrations add AddMindAtticAuth and deploy the migration (the auth schema is created alongside your existing tables; no FKs cross into app tables).
3. Wire DI + middleware + endpoints (Program.cs)
builder.Configuration.AddMindAtticVaultFiles().AddEnvironmentVariables();
builder.Services.AddMindAtticVault(builder.Configuration);
builder.Services.AddDbContextFactory<AppDbContext>(o => o.UseSqlServer(conn)); // your existing setup
builder.Services.AddScoped<AppDbContext>(sp => sp.GetRequiredService<IDbContextFactory<AppDbContext>>().CreateDbContext());
builder.Services.AddMindAtticAuthentication<AppDbContext>(builder.Configuration, o =>
{
o.AppName = "Ideas"; // per-app trust boundary
o.IsProduction = !builder.Environment.IsDevelopment();
o.ConfigureDataProtection = dp => // PROD only — fail-closed if omitted in prod
{
dp.PersistKeysToAzureBlobStorage(blobUri, cred)
.ProtectKeysWithAzureKeyVault(kvKeyUri, cred);
};
// o.ConfigureAdditionalPolicies = ab => ab.AddPolicy("MyPolicy", p => ...);
});
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await db.Database.MigrateAsync();
await scope.ServiceProvider.GetRequiredService<AuthBootstrapper>().SeedAdminAsync(); // needs Vault bootstrap-token
}
app.UseForwardedHeaders(); // configure KnownProxies/KnownNetworks (security-critical)
app.UseMindAtticAuthentication(); // authn → authz → scoped CSP nonce
app.UseAntiforgery();
app.MapMindAtticAuthEndpoints(); // /_ma-auth/*
app.MapRazorComponents<App>().AddInteractiveServerRenderMode();
app.Run();
4. Add the pages
@* /login *@
@page "/login"
@inject NavigationManager Nav
<MaLogin ReturnUrl="@_returnUrl" Error="@_error" />
@code {
string? _returnUrl; bool _error;
protected override void OnParametersSet() {
var q = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(new Uri(Nav.Uri).Query);
_returnUrl = q.TryGetValue("returnUrl", out var r) ? r.ToString() : null;
_error = q.ContainsKey("error");
}
}
Add /mfa (<MaMfaChallenge/>), /account/change-password (<MaChangePassword/>), and /account/mfa/setup (<MaMfaSetup/>, rendered @rendermode InteractiveServer). For self-service reset, add an anonymous static page at /forgot-password hosting <MaForgotPassword/> (link it from /login) and one at the ResetPath (default /account/reset) hosting <MaResetPassword Token="@Token" Error="@(Error == 1)"/> with Token/Error bound by [SupplyParameterFromQuery]; show ?reset=sent / ?reset=ok on /login; and set MindAttic:Auth:Reset:PublicBaseUrl to the app's public https origin. Protect admin pages with @attribute [Authorize(Policy = MaPolicies.Admin)] (role + MFA).
5. Email channel
AddMindAtticAuthentication registers auth email for you; there is no code to write. It reads SMTP settings in the MindAttic.Vault Notifications format (the same shape MindAttic.Psst uses):
MindAttic:Vault:Notifications:email:smtpHost / smtpPort / username / password / from MindAttic:Auth:Reset:PublicBaseUrl e.g. https://ideas.mindattic.com (reset links must be absolute)
- Dev:
%APPDATA%\MindAttic\Notifications\providers.json— include"Notifications"in theAddMindAtticVaultFilesbucket list if you pass one (it is in Vault's default list). - Prod: app settings
MindAttic__Vault__Notifications__email__smtpHost(etc.), or Key Vault references.
All five SMTP fields present ⇒ SmtpAuthEmailSender (queued, MailKit, TLS required). Anything less ⇒ the log-only LoggingAuthEmailSender, and a startup warning says email will not be delivered and names any missing field. Register your own IAuthEmailSender to override either. See OPERATIONS.md.
6. Provision secrets
Before first run, provision the Vault Security bucket: pepper.v1, bootstrap-token (and prod dp-kek/reset-token-key). See OPERATIONS.md. The hasher and bootstrap are fail-closed — the app will not start without them.
7. Migrating existing users
Run a one-time data migration mapping your old user rows → AuthUser, carrying the old hash with LegacyHashScheme = "bcrypt" (or "sha256"), so they upgrade to Argon2id+pepper on next login. Force a reset for any known-bad/hardcoded/dormant accounts. Map your old admin role onto MaRoles.Admin (the one canonical admin role name; the library has no option to rename it).