Skip to content

Commit c26ec93

Browse files
Add OIDC events documentation for ASP.NET Core
Introduced new documentation explaining OpenID Connect handler events in ASP.NET Core. Includes details on event triggers, common use cases, setup instructions, and customization examples to aid developers. #705
1 parent df9b392 commit c26ec93

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
---
2+
title: "ASP.NET Core OpenID Connect Handler Events"
3+
description: "ASP.NET Core's OpenID Connect handler events, what they are, and why you might want to use them."
4+
date: 2025-5-01
5+
sidebar:
6+
order: 60
7+
label: "OIDC Handler Events"
8+
---
9+
10+
The ASP.NET Core [OpenID Connect handler][handler] exposes events that a client can subscribe to intercept the OpenID
11+
Connect protocol flow. Understanding these events is important to understanding how to customize the OpenID Connect
12+
protocol flow from the client. We'll cover each of the events, what they are, and why you might want to subscribe to
13+
them.
14+
15+
To use the `OpenIdConnectHandler` in your client applications, you will first need to install the
16+
`Microsoft.AspNetCore.Authentication.OpenIdConnect` NuGet package.
17+
18+
```bash
19+
dotnet package add Microsoft.AspNetCore.Authentication.OpenIdConnect
20+
```
21+
22+
Followed by adding the `OpenIdConnectHandler` to your application.
23+
24+
```csharp
25+
builder.Services.AddAuthentication(options =>
26+
{
27+
options.DefaultScheme = "cookie";
28+
options.DefaultChallengeScheme = "oidc";
29+
options.DefaultSignOutScheme = "oidc";
30+
})
31+
.AddCookie("cookie", options =>
32+
{
33+
options.Cookie.Name = "__Host-bff";
34+
options.Cookie.SameSite = SameSiteMode.Strict;
35+
})
36+
.AddOpenIdConnect("oidc", options =>
37+
{
38+
options.Authority = "https://demo.duendesoftware.com";
39+
options.ClientId = "interactive.confidential";
40+
options.ClientSecret = "secret";
41+
options.ResponseType = "code";
42+
options.ResponseMode = "query";
43+
44+
options.GetClaimsFromUserInfoEndpoint = true;
45+
options.SaveTokens = true;
46+
options.MapInboundClaims = false;
47+
48+
options.Scope.Clear();
49+
options.Scope.Add("openid");
50+
options.Scope.Add("profile");
51+
options.Scope.Add("api");
52+
options.Scope.Add("offline_access");
53+
54+
options.TokenValidationParameters.NameClaimType = "name";
55+
options.TokenValidationParameters.RoleClaimType = "role";
56+
57+
});
58+
```
59+
60+
From here you can use the `options.Events` property to subscribe to the events you want to use. Let's look at each of the events in more detail.
61+
62+
## OpenID Connect Events
63+
64+
All events either occur before a request is sent to the identity provider or after a response is received from the
65+
identity provider. Understanding the direction of these events can help you determine when to subscribe to them. Let's call events coming from the identity provider **incoming** and events going to the identity provider **outgoing** for an easier understanding.
66+
67+
| **Event Name** | **Usage** |
68+
|------------------------------------------|--------------|
69+
| `OnAuthenticationFailed` | **Incoming** |
70+
| `OnAuthorizationCodeReceived` | **Incoming** |
71+
| `OnMessageReceived` | **Incoming** |
72+
| `OnRedirectToIdentityProvider` | **Outgoing** |
73+
| `OnRedirectToIdentityProviderForSignOut` | **Outgoing** |
74+
| `OnSignedOutCallbackRedirect` | **Outgoing** |
75+
| `OnRemoteSignOut` | **Incoming** |
76+
| `OnTokenResponseReceived` | **Incoming** |
77+
| `OnTokenValidated` | **Incoming** |
78+
| `OnUserInformationReceived` | **Incoming** |
79+
| `OnPushAuthorization` (**.NET 9+ only**) | **Outgoing** |
80+
81+
## Commonly Subscribed Events
82+
83+
While there are many events available in the `OpenIdConnectEvents` class, only a few are commonly subscribed. We suggest you start with the most commonly subscribed events and then subscribe to the remaining events as needed.
84+
85+
For ASP.NET Core developers, the most commonly subscribed events are:
86+
87+
1. **`OnRedirectToIdentityProvider`**: Useful for customizing login requests (e.g., appending extra parameters).
88+
2. **`OnRedirectToIdentityProviderForSignOut`**: Often required to customize the behavior of sign-out requests.
89+
3. **`OnTokenValidated`**: Frequently used to customize the claims processing or validate custom claims included in the
90+
ID token.
91+
4. **`OnUserInformationReceived`**: Sometimes used to process additional user data retrieved from the UserInfo
92+
endpoint (if enabled).
93+
94+
## Descriptions
95+
96+
### OnAuthenticationFailed
97+
98+
- **When called**: Triggered whenever an exception occurs during the authentication process. This event provides an
99+
opportunity to handle or log errors.
100+
- **How often**: Only called when an authentication error happens.
101+
- **Example use case**: Use this event to log detailed error messages or display a custom error page to the user instead
102+
of the default behavior.
103+
- **Commonly subscribed**: No, unless you need specific error-handling logic.
104+
105+
### OnAuthorizationCodeReceived
106+
107+
- **When called**: Invoked after an authorization code is received and before it is redeemed for tokens.
108+
- **How often**: Called once per successful authorization code flow request.
109+
- **Example use case**: Validate the authorization code or add extra functionality (e.g., logging or monitoring) when
110+
the code is received.
111+
- **Commonly subscribed**: Rarely, unless custom logic is required before token redemption.
112+
113+
### OnMessageReceived
114+
115+
- **When called**: Triggered when a protocol message (e.g., an authorization response, logout request) is first
116+
received.
117+
- **How often**: Called once per incoming protocol message.
118+
- **Example use case**: Inspect or modify protocol messages for debugging or to handle additional query parameters
119+
passed by the identity provider.
120+
- **Commonly subscribed**: No, unless advanced customization is needed.
121+
122+
### OnRedirectToIdentityProvider
123+
124+
- **When called**: Invoked when redirecting the user to the identity provider for authentication. You can modify the
125+
outgoing authentication request.
126+
- **How often**: Called once per user authentication attempt (e.g., a "login").
127+
- **Example use case**: Add custom query parameters to the request or modify the state parameter.
128+
- **Commonly subscribed**: Yes—often used to customize the authentication request.
129+
130+
### OnRedirectToIdentityProviderForSignOut
131+
132+
- **When called**: Triggered before redirecting the user to the identity provider to start the sign-out process.
133+
- **How often**: Called once per user sign-out request.
134+
- **Example use case**: Modify the logout request, such as appending additional parameters.
135+
- **Commonly subscribed**: Yes, if signing out requires customization.
136+
137+
### OnSignedOutCallbackRedirect
138+
139+
- **When called**: Invoked after a remote sign-out is completed and before redirecting the user to the
140+
`SignedOutRedirectUri`.
141+
- **How often**: Called once per remote sign-out.
142+
- **Example use case**: Log or perform business logic after the remote sign-out.
143+
- **Commonly subscribed**: Rarely, unless additional behavior is needed.
144+
145+
### OnRemoteSignOut
146+
147+
- **When called**: Called when a remote sign-out request is received on the `RemoteSignOutPath` endpoint.
148+
- **How often**: Called once per incoming remote sign-out request.
149+
- **Example use case**: Perform cleanup tasks such as clearing local session data upon receiving a sign-out request from
150+
the identity provider.
151+
- **Commonly subscribed**: Rarely, but important in distributed or multi-tenant systems.
152+
153+
### OnTokenResponseReceived
154+
155+
- **When called**: Triggered after an authorization code exchange is completed and the token endpoint returns tokens.
156+
- **How often**: Called once per token request.
157+
- **Example use case**: Log or debug the token response, or inspect additional data included in the token response.
158+
- **Commonly subscribed**: No, unless debugging or inspection of tokens is required.
159+
160+
### OnTokenValidated
161+
162+
- **When called**: Invoked after the ID token has been validated and an `AuthenticationTicket` has been created.
163+
- **How often**: Called once per token validation process.
164+
- **Example use case**: Add or modify claims in the `ClaimsPrincipal` or validate custom claims included in the token.
165+
- **Commonly subscribed**: Yes—this is one of the most commonly used events for customizing claims.
166+
167+
### OnUserInformationReceived
168+
169+
- **When called**: Triggered when retrieving user information from the UserInfo endpoint (if
170+
`GetClaimsFromUserInfoEndpoint = true`).
171+
- **How often**: Called once per user information fetch (e.g., per login).
172+
- **Example use case**: Extend or modify user claims based on the additional information retrieved from the UserInfo
173+
endpoint.
174+
- **Commonly subscribed**: Sometimes, if extra claims processing is required.
175+
176+
### OnPushAuthorization
177+
178+
- **When called**: Invoked before sending authorization parameters using the Pushed Authorization Request (PAR)
179+
mechanism.
180+
- **How often**: Called once per outgoing PAR-based authorization request.
181+
- **Example use case**: Modify or log pushed authorization parameters.
182+
- **Commonly subscribed**: Rarely, as this is used mainly in advanced scenarios.
183+
184+
[handler]: https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.authentication.openidconnect.openidconnecthandler?view=aspnetcore-9.0

0 commit comments

Comments
 (0)