With ASP.NET Core's AddOpenIdConnect, why is a signed-out user often signed straight back in, and how do you sign out properly?
answer
- two sessions, not one
- sign out of both schemes
- end_session_endpoint with id_token_hint
- id_token_hint needs SaveTokens
- SignedOutCallbackPath /signout-callback-oidc
basics
~20 sSigning out of the cookie scheme ends only the app's session; the identity provider's session survives, so the next challenge signs the user back in silently. Sign out of both the cookie and the OpenIdConnect schemes, which redirects to the provider's end-session endpoint.
solid answer
~40 sA user who signed in through `AddOpenIdConnect` has **two** sessions: the app's cookie and the provider's own session. `SignOutAsync("Cookies")` deletes only the cookie, so the next `[Authorize]` challenge goes to the provider, which still knows the user and redirects straight back. Sign out of **both** schemes, for example `Results.SignOut(props, [CookieAuthenticationDefaults.AuthenticationScheme, OpenIdConnectDefaults.AuthenticationScheme])`. The OIDC handler then redirects to the discovery document's `end_session_endpoint`, adding `id_token_hint` (read from the cookie, so only when `SaveTokens = true`) and `post_logout_redirect_uri` set to `SignedOutCallbackPath`, `/signout-callback-oidc`. The provider returns there, and the handler forwards to `AuthenticationProperties.RedirectUri` or `SignedOutRedirectUri` (`"/"`). Signing out of the OIDC scheme alone leaves the cookie in place.
code
csharp · 9 linesapp.MapPost("/account/logout", () =>
Results.SignOut(
new AuthenticationProperties { RedirectUri = "/signed-out" },
[CookieAuthenticationDefaults.AuthenticationScheme,
OpenIdConnectDefaults.AuthenticationScheme]))
.RequireAuthorization();
app.MapGet("/signed-out", () => "You have been signed out of the intranet.")
.AllowAnonymous();go deeper
Recall that the app and the identity provider each hold a session, and that a proper sign-out ends both.
Explain what each scheme's sign-out does, why id_token_hint depends on SaveTokens, and the order of redirects through /signout-callback-oidc.
Diagnose sign-out loops and stuck confirmation pages: unregistered post-logout URIs, a missing hint, a provider without an end-session endpoint, and GET sign-out links.
Decide what sign-out should mean across the organisation's apps: local only, provider-wide, or propagated to every app, and what each costs in user experience.
## Two sessions After an OpenID Connect sign-in, two independent sessions exist: - the **application session**: the cookie the cookie handler wrote when the OIDC handler signed the principal in; - the **provider session**: whatever the identity provider (IdP) keeps for its own login, usually its own cookie on its own domain. The classic bug is a sign-out endpoint that ends only the first one: 1. `/logout` calls `HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme)`, and the app's cookie is deleted. 2. The user lands on a protected page. Authorization fails, the default challenge scheme (OIDC) redirects to the IdP. 3. The IdP still has a valid session, so it issues a new code without asking anything and redirects back. 4. The handler signs the user in again. From the user's point of view, the button did nothing. ## Signing out of both schemes Pass both schemes to one sign-out: - Minimal APIs: `Results.SignOut(properties, [CookieAuthenticationDefaults.AuthenticationScheme, OpenIdConnectDefaults.AuthenticationScheme])` or `TypedResults.SignOut(...)`; - MVC or Razor Pages: `SignOut(properties, CookieAuthenticationDefaults.AuthenticationScheme, OpenIdConnectDefaults.AuthenticationScheme)`; - or two `HttpContext.SignOutAsync` calls, cookie first. Order does not break the hint: the OIDC handler reads the saved ID token by authenticating `SignOutScheme` against the incoming request's cookie, which deleting the cookie on the response does not affect. ## What the OIDC handler sends `OpenIdConnectHandler.SignOutAsync` does **not** delete the local cookie. It builds an end-session request: 1. It loads the discovery document and uses its `end_session_endpoint`. If the provider has none, it throws `InvalidOperationException` ("Cannot redirect to the end session endpoint, the configuration may be missing or invalid."). 2. It sets `post_logout_redirect_uri` to `SignedOutCallbackPath`, `/signout-callback-oidc` by default. That URI must be registered at the IdP. 3. It reads the ID token with `GetTokenAsync(SignOutScheme, "id_token")`. `SignOutScheme` defaults to `SignInScheme`, the cookie. The token is there only if `SaveTokens = true`; without it no `id_token_hint` is sent, and many providers then show a confirmation page or ignore the redirect URI. 4. It raises `OnRedirectToIdentityProviderForSignOut`, where you can add parameters or take over the response. 5. It redirects (or form-posts, depending on `AuthenticationMethod`). When the IdP sends the browser back to `/signout-callback-oidc`, the handler raises `OnSignedOutCallbackRedirect` and then redirects to `AuthenticationProperties.RedirectUri`, or to `SignedOutRedirectUri`, which defaults to `"/"`. ## Paths at a glance | Option | Default | Used for | |---|---|---| | `CallbackPath` | `/signin-oidc` | sign-in response from the IdP | | `SignedOutCallbackPath` | `/signout-callback-oidc` | the IdP's return after sign-out | | `RemoteSignOutPath` | `/signout-oidc` | a sign-out request the IdP sends to the app | | `SignedOutRedirectUri` | `"/"` | where the user ends up when no `RedirectUri` was given | Requests to `RemoteSignOutPath` raise `OnRemoteSignOut` and, if the request's session and issuer checks pass, sign the user out of `SignOutScheme`. How the provider fans a logout out to every app, and the back-channel variant, is a federation design topic of its own. ## Social providers differ Only handlers that implement `IAuthenticationSignOutHandler` can sign out. `OpenIdConnectHandler` does; the OAuth-based handlers such as `AddGoogle` do not. Calling `SignOutAsync("Google")` throws `InvalidOperationException`, so a Google-based app signs out of its cookie only. ## Checking it in the browser A correct sign-out shows this sequence in the browser's network panel: 1. `POST /account/logout`, answered with a redirect to the provider's end-session endpoint carrying `id_token_hint`, `post_logout_redirect_uri` and `state`; 2. the provider's own pages, which end its session; 3. `GET /signout-callback-oidc`, answered with a redirect to `/signed-out`; 4. on the next visit to a protected page, the provider's **login form**, not an immediate return. If step 4 skips the form, the provider session survived: either the OIDC scheme was never signed out, or the provider ignored the request. ## Checklist - Sign out of the cookie **and** the OIDC scheme in one request. - Set `SaveTokens = true` so `id_token_hint` is sent. - Register `/signout-callback-oidc` at the provider for every host. - Make sign-out a POST so another site cannot trigger it with a link. - If a fresh login prompt is the goal rather than a real sign-out, send `prompt=login` on the next challenge instead.
- After sign-out the provider shows "Are you sure you want to sign out?" and never returns to the app. What is the likely cause?No `id_token_hint` was sent, because `SaveTokens` is `false` and the handler found no `id_token` in the cookie, or `/signout-callback-oidc` is not registered as a post-logout redirect URI. Many providers honour `post_logout_redirect_uri` only with a valid hint and a registered URI.
- What does OnRedirectToIdentityProviderForSignOut let you do?It runs just before the end-session redirect with the `OpenIdConnectMessage` in `context.ProtocolMessage`, so you can add provider-specific parameters such as `client_id`, change the target, or call `context.HandleResponse()` to skip the redirect, for example when the provider has no end-session endpoint.
- Deleting the app's auth cookie also ends the user's session at the identity provider.
- Signing out of the OpenIdConnect scheme alone also deletes the local cookie.
- The handler sends id_token_hint whether or not SaveTokens is on.
- With default options, registering only the home page as the post-logout redirect URI is enough.
- Calling SignOutAsync with an AddGoogle scheme signs the user out of Google.