Microsoft Entra ID answers “Who signed in?” but your app still needs to work out which local account belongs to that person.

An email address is useful for sending an invitation, but it’s a poor key for matching a sign-in to a local account. Email addresses can be changed or reassigned, and in Entra ID, the email claim isn’t guaranteed to be verified. An administrator in any tenant can set a user’s email attribute to an address they don’t own.

This isn’t just theoretical. In 2023, Microsoft documented an account-takeover risk in applications that trusted the email claim to identify users. Authentication worked and identified the right person, but the application gave them access to the wrong account.

The examples below apply to ASP.NET Core, but the principles apply to any stack.

Use a stable identity key

When a user signs in, look up the local account using two claims from the validated token: tid, the Entra Tenant ID, and oid, the user’s Object ID within that tenant. Store the pair as the external identity key and enforce its uniqueness in the database. The same person has a different oid in each tenant they’re a guest in, so oid alone isn’t enough once more than one tenant is involved.

In a strictly single-tenant application, oid alone is sufficient as long as you actually reject tokens from other tenants. Consider storing tid anyway - many single-tenant apps become multi-tenant later, and changing an identity key after the fact is painful.

Avoid using sub as the key. It’s stable, but it’s unique per application, so the same user gets a different value if you ever add a second app registration.

Keep email, username, and display name as profile data. A user changing their name or address shouldn’t lose access, and a person inheriting an email address from a previous employee shouldn’t inherit their account. Microsoft’s ID token claims reference explains which identifiers are suitable, and it’s guidance on migrating away from email claims show the tid + oid pattern.

Let the middleware validate the token

Configure the authentication middleware for the issuer, audience, and tenants that you accept, then do an account lookup after validation has finished.

In both web apps using OIDC and APIs using bearer tokens, the signature, issuer, audience, and lifetime checks run before the OnTokenValidated event.

If you customize OnTokenValidated with Microsoft.Identity.Web, register your callback after the library’s setup, capture the existing handler, await it first, then check context.Result. Microsoft provides a short example of preserving existing handlers.

Decide what happens on first sign-in

When a returning user signs in, the app finds their local account using the tid and oid from the validated token. However, when someone signs in for the first time, no local account is linked to that identity yet, so the app may need to create the link. Don’t assume that an existing account with a matching email is theirs.

  • Open registration: Create a new local account for the authenticated identity. Don’t silently merge it with an existing account because the emails match.
  • Invite-only registration: Use an expiring, single-use token tied to a specific pending user. The invitation selects which user to activate; the validated Entra identity tells you who is accepting. If the invitation has restrictions, verify those too. For example, if it was issued to a particular customer, check that the tid matches that customer’s tenant. Save the link and mark the invitation as redeemed in one atomic operation, so that a subsequent request can’t claim it.
  • Linking to an existing account: Require independent proof that the person controls the account, such as signing in to that account, or use a mapping approved by an administrator. A matching email address, username, or alias does not count as proof.

Use the ASP.NET Identity building blocks

If you use ASP.NET Identity for local accounts, UserManager.AddLoginAsync records an external login against a local user. It stores the link, but doesn’t decide whether the link should exist. That decision belongs to the flow above.

By default, the external-login flow takes the provider key from NameIdentifier claim, which the standard claim mapping usually fills from sub. Build the key yourself from tid and oid instead.

For invitations tied to a pending Identity user, GenerateUserTokenAsync and VerifyUserTokenAsync can handle token generation and verification. Use an expiring provider and an invitation-specific purpose.

Note that verifying a token doesn’t mark it as redeemed, so your app needs to record redemption and reject reuse. Do the following in one database transaction:

  1. Mark the invitation as redeemed only if it hasn’t been redeemed already, and confirm that exactly one row changed.
  2. Call AddLoginAsync
  3. Call UpdateSecurityStampAsync, which invalidates other outstanding tokens for that user.

Three regression tests are worth keeping:

  1. The same identity with a new email opens the same account
  2. A different identity with the old email does not
  3. Two simultaneous attempts to redeem one invitation create at most one link

Remember that the identity key for a user is tid + oid, and that email is only a property of the user profile. Create a link only when you have proof that the person should have access to that account, not because the email address matches.

Want to read more?

Descope did a great writeup on nOAuth: How Microsoft OAuth Misconfiguration Can Lead to Full Account Takeover.