Overview¶
When users authenticate with Open.IdentityServer, a session is created to track the logged in user. By default this will be done by storing this state in cookies in the user browser. This approach of storing session state in a cookie can work well for many scenarios but does have some drawbacks.
No Tracking Active Sessions - There is no way to track active sessions, and how many users are currently logged in.
No Immediate Revocation - There will be no process of Immediate session revocation on the server side-session cookie will be valid till it expires, or they log out.
No Sign-Out Coordination - Coordinating sign-outs from Open.IdentityServer with connected clients is less relable without server tracking of active sessions.
Open.IdentityServer Server-Side Sessions solves these issues by storing the contents of this cookie in a server side data store. This gives Open.IdentityServer the ability to:
Provide APIs for managing and querying active user sessions
Support for explicit session revocation regardless of the cookie state in the browser
Storing session data server side, so browser cookie only contains an ID for the session nothing more
Getting Started¶
Database schema
Ensure your database is in the correct state. If you are coming from Duende IdentityServer, there is nothing further to do - the schema is already compatible. If you are migrating from IdentityServer4 and have not yet updated your schema to match the Open.IdentityServer schema, you will need to do this first. See migration from IdentityServer4 for details.
Enable server-side sessions
Call
.AddServerSideSessions()when configuring Open.IdentityServer:builder.Services.AddIdentityServer() .AddServerSideSessions();
Configure a session store
AddServerSideSessionsrequires an implementation ofIServerSideSessionStoreto persist session data. If you are using Entity Framework Core, this is provided automatically when you configure the operational store:builder.Services.AddIdentityServer() .AddServerSideSessions() .AddOperationalStore(options => { options.ConfigureDbContext = b => b.UseSqlServer("ConnectionString"); });
If you are not using the built-in EF Core store, you will need to provide your own
IServerSideSessionStoreimplementation.
(Optional) Configure additional session options
You can customize behavior via
ServerSideSessionOptions, such as how often sessions are checked for expiration in the background, or coordinating this with your sign-in cookie expiration:builder.Services.AddIdentityServer(options => { // Other options... options.ServerSideSessions.ExpiredSessionsTriggerBackchannelLogout = true; options.ServerSideSessions.RemoveExpiredSessions = true; options.ServerSideSessions.RemoveExpiredSessionsFrequency = TimeSpan.FromSeconds(10); options.ServerSideSessions.FuzzExpiredSessionsFrequency = true; options.ServerSideSessions.RemoveExpiredSessionsBatchSize = 100; });
Management Interface¶
Open.IdentityServer has a built-in session management interface, ISessionManagementService, that allows you to query existing sessions and remove sessions. When you configure Open.IdentityServer to use server-side sessions, a default implementation is registered for this interface.
The interface provides two methods:
Task<QueryResult<UserSession>> ISessionManagementService.QuerySessionsAsync(SessionQuery? filter, CancellationToken ct = default)Filters sessions using the
SessionQueryobject. The filter is optional; default values are used when it is not provided.Task ISessionManagementService.RemoveSessionsAsync(RemoveSessionsContext context, CancellationToken ct = default)Removes sessions using the
RemoveSessionsContextobject. The context allows you to control which sessions are removed and what actions are taken as part of the removal.
Data Types¶
The following types are used by ISessionManagementService to query and manage sessions.
SessionQuery¶
SessionQuery is used to filter results when calling QuerySessionsAsync.
Property |
Type |
Description |
|---|---|---|
|
|
Optional selector for current page location, contains identifier for the first element and last element in page of results. e.g. ‘0,24’ |
|
|
Specifies if instead of getting next page should get the previous page that ResultsToken identifies. Only valid if ResultsToken is specified. |
|
|
Specifies the count requested per page, defaults to 25 |
|
|
Filters sessions with a specific subject id |
|
|
Filters sessions with a specific session id |
|
|
Filters sessions with a specific display name |
QueryResult<T>¶
QueryResult<T> wraps paged results returned from QuerySessionsAsync.
Property |
Type |
Description |
|---|---|---|
|
|
Token identifying the current page of results. Contains the first element and last element in page of results. e.g. ‘0,24’ |
|
|
Specifies if there is a page of results before this page |
|
|
Specifies if there is a page of results after this page |
|
|
Total sessions accross all pages of results |
|
|
Total number of pages for query result |
|
|
Page numer of the current result collection |
|
|
The collection of results for the current page |
UserSession¶
UserSession represents a single server-side session record.
Property |
Type |
Description |
|---|---|---|
|
|
The subject (user) identifier associated with the session. |
|
|
The unique identifier for the session |
|
|
An optional display name for the user |
|
|
The date and time the session was created. |
|
|
The date and time the session was last renewed |
|
|
The date and time the session expires, if applicable |
|
|
The issuer of the authentication ticket |
|
|
The clients associated with the session |
|
|
The user sessions suthentication ticket object |
RemoveSessionsContext¶
RemoveSessionsContext controls which sessions are removed and what side effects occur when calling RemoveSessionsAsync.
Property |
Type |
Description |
|---|---|---|
|
|
Optional if SessionId has value, limits removal to a specific subject id |
|
|
Optional if SubjectId has value, limits removal to a specific session id |
|
|
Optionally limits removal to sessions for specific clients |
|
|
Whether to trigger session entity removal from the database |
|
|
Whether to trigger backchannel logout notifications for removed sessions |
|
|
Whether to trigger session token removal |
|
|
Whether to trigger session consent removal |