ASP.NET Core MVC Advanced

Sign in with Google and Microsoft accounts

Letting users sign in with Google or Microsoft saves them a password and saves you from storing one. This lesson explains how OpenID Connect sign-in works, registers an app with a provider, configures ASP.NET Core, and links external accounts to your own users.

Sign in with Google
  1. StartUser clicks GoogleYour site calls Challenge("Google")
  2. Step 2Google sign-in pageUser signs in and consents
  3. Step 3Callback /signin-googleCode exchanged for the user's identity
  4. Step 4Your appFinds or creates the local user
  5. EndCookie issuedUser is signed in to your site

How it works

Your site redirects the user to the provider's sign-in page. After they sign in and agree, the provider redirects back to a callback URL on your site with a code, which ASP.NET Core exchanges for the user's identity. You never see their password.

Each provider needs an app registration that gives you a client ID and client secret, and where you list your callback URL, such as https://localhost:5001/signin-google.

Configuring providers

Store the client secrets in user secrets during development and in Key Vault or app settings in production.

C#
builder.Services.AddAuthentication(o =>
    {
        o.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    })
    .AddCookie()
    .AddGoogle(o =>
    {
        o.ClientId = builder.Configuration["Auth:Google:ClientId"]!;
        o.ClientSecret = builder.Configuration["Auth:Google:ClientSecret"]!;
    })
    .AddMicrosoftAccount(o =>
    {
        o.ClientId = builder.Configuration["Auth:Microsoft:ClientId"]!;
        o.ClientSecret = builder.Configuration["Auth:Microsoft:ClientSecret"]!;
    });

Starting sign-in and handling the result

C#
[HttpPost, ValidateAntiForgeryToken]
public IActionResult ExternalLogin(string provider) =>          // "Google" or "Microsoft"
    Challenge(new AuthenticationProperties { RedirectUri = Url.Action(nameof(ExternalDone)) }, provider);

public async Task<IActionResult> ExternalDone()
{
    var email = User.FindFirstValue(ClaimTypes.Email);
    var key = User.FindFirstValue(ClaimTypes.NameIdentifier);   // stable id from the provider
    // Find or create your own user record by (provider, key), not by email alone
    var user = await users.FindOrCreateExternalAsync(User.Identity!.AuthenticationType!, key!, email);
    return RedirectToAction("Index", "Home");
}

Good to know

  • With ASP.NET Core Identity, the template's external login pages and the AspNetUserLogins table do the linking for you.
  • Link accounts by the provider's user id, not just the email; emails can change or be reused.
  • Ask only for the scopes you need (usually openid, profile and email).
  • Add both localhost and production callback URLs to the provider registration.
  • For company sign-in with Microsoft Entra ID, use Microsoft.Identity.Web.

Interview question

Why should you link external logins by the provider's user ID rather than by email address?

The provider's subject identifier is stable and unique for that user in that provider, while email addresses can change, be recycled for a new person, or be unverified at some providers. Matching only on email could let someone take over another account. Store the pair of provider name and provider key, as Identity's AspNetUserLogins table does.

Your progress is saved only in this browser.

Want the full details? Read the official Microsoft documentation for this topic.