RO EN

OAuth2 & OpenID Connect explained clearly

OAuth2 & OpenID Connect explained clearly
Doru Bulubasa
14 April 2026

How Modern Authentication Works in .NET Applications

Series: Security by Design in .NET: From JWT to Certificate-based Authentication

If you have used:

  • Login with Google

  • Login with Microsoft

  • Identity Server

  • Keycloak

๐Ÿ‘‰ then you have used OAuth2 or OpenID Connect (probably without knowing exactly how it works).


๐Ÿง  The main problem

Many believe:

OAuth2 = authentication

โŒ FALSE


๐Ÿ”‘ What is OAuth2

OAuth2 is:

๐Ÿ” an authorization protocol

It allows an application to access resources on behalf of a user.


Simple example

โ€œLogin with Googleโ€

Actually:

๐Ÿ‘‰ your application requests access to:

  • email

  • profile

Google says:

โ€œOK, but only if the user consents.โ€

Result

You receive:
{
  "access_token": "abc123"
}

This token is used for:

โœ” API access

โŒ NOT to know who the user is (correct)


๐Ÿง  What is OpenID Connect (OIDC)

OpenID Connect is:

๐Ÿ”‘ a layer on top of OAuth2 for authentication

It adds the concept of:
{
  "id_token": "jwt_here"
}


ID Token

It is a JWT that contains:

{
  "sub": "user123",
  "email": "user@email.com",
  "name": "John Doe"
}

๐Ÿ‘‰ this tells you who the user is


โš”๏ธ OAuth2 vs OpenID Connect

Feature

OAuth2

OpenID Connect

Purpose

Authorization

Authentication

Token

Access Token

ID Token + Access Token

User info

โŒ Not guaranteed

โœ” Yes

JWT

Not mandatory

Yes (ID Token)

 


๐Ÿ” Main flow: Authorization Code Flow

Steps:

1๏ธโƒฃ User is redirected to the provider

2๏ธโƒฃ Authenticates

3๏ธโƒฃ Receives an authorization code

4๏ธโƒฃ The application exchanges it for:

  • access token

  • id token (OIDC)


๐Ÿ” PKCE (important in 2026)

PKCE protects against code interception. Used in:

โœ” SPA

โœ” mobile apps

โœ” Blazor WebAssembly


๐Ÿงฑ How it integrates in .NET

ASP.NET Core natively supports OIDC.


Example:

builder.Services.AddAuthentication(options =>
{
    options.DefaultScheme = "Cookies";
    options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
    options.Authority = "https://your-identity-server";

    options.ClientId = "client-id";
    options.ClientSecret = "secret";

    options.ResponseType = "code";

    options.SaveTokens = true;
});


๐Ÿ”— Common providers

You can integrate with:


๐Ÿง  When you need OAuth2 / OIDC

โœ” enterprise applications

โœ” login with Google / Microsoft

โœ” microservices

โœ” SSO (Single Sign-On)


โŒ When you DO NOT need it

If you have:

  • small application

  • simple login

  • no external providers

๐Ÿ‘‰ you can just use Identity + JWT


๐Ÿšจ Common mistakes

โŒ using OAuth2 for authentication without OIDC

โŒ not validating ID Token

โŒ insecure token storage

โŒ missing PKCE

โŒ using implicit flow (deprecated)


๐Ÿงฑ Modern architecture

Client (Blazor / SPA)
        ↓
OIDC Provider (Keycloak / IdentityServer)
        ↓
Access Token (API)
        ↓
Resource Server (ASP.NET Core API)


๐Ÿ”ฅ Best Practices

โœ” Use Authorization Code Flow

โœ” Enable PKCE

โœ” Validate ID Token

โœ” Do not expose access token in frontend

โœ” Use HTTPS mandatory

โœ” Configure scopes correctly


๐ŸŽฏ Conclusion

๐Ÿ‘‰ OAuth2 = authorization

๐Ÿ‘‰ OpenID Connect = authentication

Together they form:

๐Ÿ” the modern authentication standard in enterprise applications