Skip to content

Commit c49d4e5

Browse files
authored
Merge pull request #979 from DuendeSoftware/mb/schemes
Scheme Names
2 parents 5fb4165 + 458aba1 commit c49d4e5

File tree

1 file changed

+78
-0
lines changed
  • src/content/docs/identityserver/aspnet-identity

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
title: "Authentication Schemes and Cookies"
3+
description: "Understanding the authentication schemes and cookies used by Duende IdentityServer, especially when integrated with ASP.NET Identity."
4+
sidebar:
5+
order: 5
6+
---
7+
8+
Authentication in ASP.NET Core is organized into [authentication schemes](https://learn.microsoft.com/en-us/aspnet/core/security/authentication/#authentication-scheme). A scheme is a name that corresponds to an authentication handler and its configuration options.
9+
IdentityServer relies on several specific schemes for different purposes, and understanding them is crucial, especially when integrating with ASP.NET Identity.
10+
11+
## Cookie Schemes
12+
13+
When a user logs in, their identity is established and persisted across requests using a cookie. IdentityServer uses a primary authentication cookie to track the user's session.
14+
15+
### Standalone IdentityServer
16+
17+
When using IdentityServer without ASP.NET Identity, the default cookie scheme is named `"idsrv"`, though we recommend using the constant `IdentityServerConstants.DefaultCookieAuthenticationScheme` in your code if you ever need it.
18+
19+
The default cookie scheme is configured by default in `AddIdentityServer()`, which sets up the cookie authentication handler with this scheme name. This cookie is essential for:
20+
21+
- maintaining the user's authenticated session
22+
- supporting single sign-on (SSO)
23+
- managing sign-out
24+
25+
### With ASP.NET Identity
26+
27+
When you integrate ASP.NET Identity, for example using `AddAspNetIdentity<TUser>()`, the configuration changes to align with ASP.NET Identity's defaults.
28+
29+
In this scenario, the main authentication cookie scheme is not `"idsrv"`. Instead, it uses the ASP.NET Identity default scheme name: `"Identity.Application"` (or the `IdentityConstants.ApplicationScheme` constant).
30+
31+
This is a common point of confusion. ASP.NET Identity registers its own cookie handlers, and `AddAspNetIdentity` configures IdentityServer to use them. This means:
32+
33+
1. **Login UI:** When you call `HttpContext.SignInAsync`, you must use the correct scheme. If you use the `SignInManager<TUser>` provided by ASP.NET Identity, it automatically uses `"Identity.Application"`.
34+
2. **Configuration:** If you need to configure cookie options (like expiration or sliding expiration), you must configure the options for `"Identity.Application"`, not `"idsrv"`.
35+
36+
```csharp
37+
// Program.cs
38+
services.ConfigureApplicationCookie(options =>
39+
{
40+
// The default ("Identity.Application")
41+
options.Cookie.Name = IdentityConstants.ApplicationScheme;
42+
43+
// Configure other options here...
44+
options.ExpireTimeSpan = TimeSpan.FromHours(1);
45+
options.SlidingExpiration = true;
46+
});
47+
```
48+
49+
## Other Important Schemes
50+
51+
Besides the main application cookie, IdentityServer uses other schemes for specific features.
52+
53+
### External Authentication (e.g., Google, OIDC)
54+
55+
When a user signs in with an external provider (like Google or another OIDC provider), the result of that remote authentication is temporarily stored in an "external" cookie. This allows your login logic to read the claims from the external provider before fully signing the user into your main local session.
56+
57+
- **Standalone Default:** `"idsrv.external"` (Constant: `IdentityServerConstants.ExternalCookieAuthenticationScheme`)
58+
- **With ASP.NET Identity:** `"Identity.External"` (Constant: `IdentityConstants.ExternalScheme`)
59+
60+
### Check Session Cookie
61+
62+
IdentityServer session management requires a separate cookie to monitor the session state without sending the large authentication cookie.
63+
The [User Session Service](/identityserver/reference/services/user-session-service.md) manages this cookie.
64+
65+
- **Default Name:** `"idsrv.session"` (Constant: `IdentityServerConstants.DefaultCheckSessionCookieName`). This often remains consistent, but verify your specific configuration.
66+
67+
## Summary Table
68+
69+
| Feature | Standalone IdentityServer | With ASP.NET Identity |
70+
| :----------------------- | :---------------------------------------------------------------------------------------- | :------------------------------------------------------------------- |
71+
| **Main Auth Cookie** | `"idsrv"`<br/>(`IdentityServerConstants.DefaultCookieAuthenticationScheme`) | `"Identity.Application"`<br/>(`IdentityConstants.ApplicationScheme`) |
72+
| **External Auth Cookie** | `"idsrv.external"`<br/>(`IdentityServerConstants.ExternalCookieAuthenticationScheme`) | `"Identity.External"`<br/>(`IdentityConstants.ExternalScheme`) |
73+
| **Typical Usage** | `HttpContext.SignInAsync(IdentityServerConstants.DefaultCookieAuthenticationScheme, ...)` | `SignInManager.SignInAsync(...)` |
74+
75+
## Common Pitfalls
76+
77+
- **Mixing Schemes:** Attempting to `SignOutAsync("idsrv")` when ASP.NET Identity is in use will have no effect on the actual `"Identity.Application"` cookie, leaving the user logged in. Always use the constants or the helper services (like `SignInManager`) that match your configuration.
78+
- **Cookie Configuration:** Setting options on the default authentication scheme (which might differ from the effective cookie scheme) or configuring the wrong named options instance will result in settings (like `Cookie.SameSite` or `ExpireTimeSpan`) being ignored.

0 commit comments

Comments
 (0)