Introduction
OpenID Connect (OIDC) is an authentication protocol built on top of OAuth 2.0. While OAuth 2.0 focuses on authorization (what a user or app is allowed to do), OIDC adds an extra layer by providing authentication (proving who the user is).
OIDC introduces the concept of an ID Token, which is a JSON Web Token (JWT) that securely contains user identity information (such as name, email, and userId). This allows applications not only to authorize actions but also to identify the authenticated user.
In short:
Why OIDC is Important in Modern .NET Applications
Centralized Authentication
With OIDC, .NET applications can rely on trusted identity providers (e.g., Azure Entra ID, Auth0, Google Identity, Okta) instead of managing usernames and passwords themselves.
Single Sign-On (SSO)
Users can log in once and access multiple .NET apps or services without re-entering credentials.
Improved Security
No need to store or manage user credentials.
Tokens are short-lived and securely transmitted.
Built-in protection against common attacks (e.g., replay attacks, token forgery).
Standards-Based and Future-Proof
OIDC is widely adopted and interoperable, making it easier to integrate .NET apps with cloud providers, APIs, and third-party services.
Seamless Integration with .NET 8
.NET 8 provides built-in support for OIDC via middleware packages like:
Implementing OIDC in a .NET 8 Application
Below is a simple example of integrating OIDC in a .NET 8 Web API or MVC app.
Step 1. Configure Authentication in Program.cs
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
options.Authority = "https://login.microsoftonline.com/{tenantId}/v2.0"; // Identity Provider
options.ClientId = "your-client-id";
options.ClientSecret = "your-client-secret";
options.ResponseType = "code"; // Authorization Code Flow
options.SaveTokens = true; // Save tokens for later use
});
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.MapGet("/secure", (ClaimsPrincipal user) => $"Hello {user.Identity?.Name}, you are authenticated!")
.RequireAuthorization();
app.Run();
Step 2. Run and Authenticate
When users try to access /secure, they’ll be redirected to the Identity Provider (e.g., Azure AD).
After login, the app receives an ID Token (who the user is) and an Access Token (what the user can access).
Real-World Use Cases (Made Simple)
Let’s look at three everyday scenarios where OIDC shines. Imagine these as real-life stories instead of just technical steps.
Enterprise Apps: One Login for Many Tools
Think of a big company where employees use many tools—HR portal, project tracker, reporting dashboard. Without OIDC, they’d need a separate username and password for each tool. That’s messy!
With OIDC + Azure Entra ID (formerly Azure AD):
Steps
The employee opens any internal tool (e.g., HR portal).
The app redirects them to Azure Entra ID for login.
The employee signs in once with their work email and password.
Azure Entra ID authenticates and issues tokens.
The employee is automatically signed into all other allowed apps (thanks to SSO).
=> IT teams can add rules like multi-factor authentication (MFA), block risky logins, or lock accounts quickly.
=> Result: Less password hassle for employees, and stronger security for the organization.
APIs & Microservices: Services Talking Safely
Modern apps often run as microservices—small apps working together. For example, an Order Service may talk to a Payment Service. But how do they trust each other?
This is where OIDC helps:
Steps
The Order Service requests an access token from the Identity Provider.
The token (JWT) is attached to the request sent to the Payment Service.
The Payment Service validates the token: Is it issued by the right authority? Is it expired? Is the audience correct?
If valid, the Payment Service processes the request.
If not, the request is rejected.
=> Imagine it like a concert ticket—the token proves you’re allowed in. Without it, you’re not getting past security!
Customer Portals: Easy Social Logins
Now think of apps you use daily—shopping sites, online courses, or SaaS dashboards. Do you really want to create another new account each time? Probably not.
OIDC makes life easier by letting apps connect to social logins:
Steps
The user clicks “Sign in with Google/Microsoft/GitHub/Facebook”.
They’re redirected to the provider’s login page.
The user signs in there (e.g., Google account).
The provider issues an ID Token back to the app with verified details.
The app grants access—no new password required.
=> Example: A SaaS dashboard built in .NET 8 could let developers log in with GitHub, and business users log in with Microsoft accounts—all while the app stays secure using standard OIDC token checks.
Summary
OpenID Connect (OIDC) is the modern standard for securing .NET applications by providing both authentication and authorization in a single, streamlined protocol. With .NET 8’s built-in support, developers can easily integrate OIDC, enabling secure logins, seamless SSO, and compliance with industry best practices.