Course contentsShow
← C#
Lesson 1282 of 1,90811. Identity, Security, and ClaimsFree lesson

Passwordless Authentication Methods

Implement Magic Links or FIDO2-based passwordless login mechanisms.

In this lesson, you will learn how to implement "Magic Links" as a reliable, user-friendly passwordless authentication mechanism in ASP.NET Core.

The Magic Link Approach

Rather than asking users to remember complex passwords—which are susceptible to credential stuffing—Magic Links rely on an out-of-band verification channel. When a user logs in, they provide their email address, and the server generates a cryptographically secure, time-limited token. This token is sent via a unique URL to their inbox. When the user clicks the link, the application validates the token and authenticates the user’s session.

Implementation Logic

To implement this in C#, you need to generate a token, associate it with the user, and handle the verification endpoint. You can leverage the IDataProtectionProvider to ensure the token is tamper-proof:

// Generating the token
string token = _protector.Protect(user.Id.ToString() + "|" + DateTime.UtcNow.AddMinutes(15).Ticks);
string magicLink = $"https://yourapp.com/verify?token={HttpUtility.UrlEncode(token)}";

// Verifying the token
string unprotected = _protector.Unprotect(token);
var parts = unprotected.Split('|');
if (DateTime.UtcNow.Ticks < long.Parse(parts[1])) 
{
    await _signInManager.SignInAsync(user, isPersistent: false);
}

Critical Security Considerations

While convenient, Magic Links carry specific risks:

  1. Token Lifespan: Keep the window short (e.g., 5–15 minutes). Once a link is used, it must be invalidated immediately to prevent replay attacks.
  2. Email Security: Because email is not an inherently secure transport, ensure your communication uses TLS, and consider combining this with a secondary factor if the account requires higher-assurance access.
  3. Information Leakage: Do not reveal whether an email address exists in your system during the login request; always return a generic "If an account exists, a link has been sent" response to prevent user enumeration.

By removing passwords, you shift the security burden from the user's memory to the integrity of the email delivery channel, significantly reducing the success rate of automated brute-force attacks.

Key Takeaway: Magic Links enhance security by replacing vulnerable credentials with time-limited, single-use tokens sent through a trusted out-of-band communication channel.