Skip to content

Commit db106df

Browse files
authored
Merge pull request #827 from DuendeSoftware/wca/iticketstore-and-cookie-size
Describe using ITicketStore or server-side sessions to reduce cookie size
2 parents 9abe43d + e25b4ab commit db106df

File tree

1 file changed

+60
-0
lines changed
  • src/content/docs/identityserver/troubleshooting

1 file changed

+60
-0
lines changed

src/content/docs/identityserver/troubleshooting/index.mdx

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,64 @@ When dealing with external authentication, you may want to set `MapInboundClaims
279279

280280
When dealing with external authentication, you may want to implement `OnTicketReceived` to reduce the size of the cookie. This is a callback that is invoked after the external authentication process is complete. You can use this callback to remove any claims that are not needed by your solution.
281281

282+
### Use Server-side Sessions
283+
284+
If you have a Business Edition or higher license for IdentityServer, then you can use [server-side sessions][2] to store the
285+
user's session data in a data store instead of in the cookie. This will greatly reduce the size of the cookie while allowing you to store more data in the session.
286+
287+
### Implement a Custom `ITicketStore` to Reduce Cookie Size
288+
289+
When configuring the cookie authentication handler, you can provide a custom `ITicketStore` implementation to store the
290+
authentication ticket data server-side instead of in the cookie.
291+
292+
```csharp
293+
// Program.cs
294+
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
295+
.AddCookie(options =>
296+
{
297+
// Use your custom ITicketStore implementation:
298+
options.SessionStore = new CustomTicketStore();
299+
});
300+
301+
```
302+
303+
And then implement the `ITicketStore` interface to store the ticket data in a database or other storage mechanism.
304+
305+
```csharp
306+
// CustomTicketStore.cs
307+
public class CustomTicketStore : ITicketStore
308+
{
309+
public Task<string> StoreAsync(AuthenticationTicket ticket)
310+
{
311+
// Implement your logic to store the ticket data
312+
// Return a unique identifier for the stored ticket
313+
}
314+
315+
public Task<AuthenticationTicket> RetrieveAsync(string key)
316+
{
317+
// Implement your logic to retrieve the ticket data by key
318+
}
319+
320+
public Task RemoveAsync(string key)
321+
{
322+
// Implement your logic to remove the ticket data by key
323+
}
324+
325+
public Task RenewAsync(string key, AuthenticationTicket ticket)
326+
{
327+
// Implement your logic to renew the ticket data by key
328+
}
329+
}
330+
```
331+
332+
:::caution[ITicketStore and Dependency Injection]
333+
When using the `AddCookie` method to configure the cookie authentication handler, you cannot use dependency injection
334+
to resolve a service and its dependencies for `ITicketStore`.
335+
336+
To work around this limitation, you can create a custom `IPostConfigureOptions<CookieAuthenticationOptions>` implementation
337+
like we did for [Server-Side Sessions][2], which uses [a shim][3] to inject an `IHttpContextAccessor` into the actual `ITicketStore` service.
338+
:::
339+
282340
## URL and Query String Size Limits and Management
283341

284342
While most browsers currently support URLs longer than 2000 characters, web servers may still return an error status code
@@ -419,3 +477,5 @@ When an application runs without an active user profile, any private key materia
419477
Even loading a certificate can fail, since the load operation could attempt to store the private key material in the user profile.
420478

421479
[1]: https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.x509keystorageflags
480+
[2]: /identityserver/ui/server-side-sessions
481+
[3]: https://github.com/DuendeSoftware/products/blob/main/identity-server/src/IdentityServer/Configuration/DependencyInjection/PostConfigureApplicationCookieTicketStore.cs

0 commit comments

Comments
 (0)