Advertisement
Advanced ASP.NET Core, Authentication

How would you implement passwordless authentication (like a magic link sent via email) in ASP.NET Core?

Short answer

A magic link flow generates a short-lived, single-use, cryptographically random token associated with the user's email, stored server-side (database or cache) with an expiration, emailed as a URL parameter; when clicked, the endpoint validates the token (checking existence…

Save ASP.NET Core mock interview
Advertisement
AModel answer
A magic link flow generates a short-lived, single-use, cryptographically random token associated with the user's email, stored server-side (database or cache) with an expiration, emailed as a URL parameter; when clicked, the endpoint validates the token (checking existence, expiration, and single-use consumption), and if valid, calls SignInAsync to establish an authenticated session -- effectively substituting 'possession of the emailed link' for a traditional password.
app.MapGet("/auth/magic-link", async (string token, IMagicLinkStore store, HttpContext ctx) => {
    var email = await store.ValidateAndConsumeAsync(token);  // single-use, expires quickly
    if (email is null) return Results.Unauthorized();
    var principal = BuildPrincipalForUser(email);
    await ctx.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
    return Results.Redirect("/dashboard");
});
Real-world example
A B2C SaaS product offers passwordless login via emailed magic links to reduce signup friction and eliminate password-related support tickets, using a database-backed single-use token store to ensure each link can only be used once.
Likely follow-ups
  • How do you prevent a magic link from being forwarded or intercepted and reused maliciously?
  • What's the UX trade-off of passwordless authentication for users without reliable email access?
Related topics