Skip to content

Session Management

Server-side sessions enable secure and efficient storage of session data, allowing flexibility through custom implementations of the IUserSessionStore interface. This ensures adaptability to various storage solutions tailored to your application’s needs.

If using the server-side sessions feature, you will need to have a store for the session data. An Entity Framework Core based implementation of this store is provided.

If you wish to use some other type of store, can implement the IUserSessionStore interface:

/// <summary>
/// User session store
/// </summary>
public interface IUserSessionStore
{
/// <summary>
/// Retrieves a user session
/// </summary>
/// <param name="key"></param>
/// <param name="ct">A token that can be used to request cancellation of the asynchronous operation.</param>
/// <returns></returns>
Task<UserSession?> GetUserSessionAsync(UserSessionKey key, CT ct = default);
/// <summary>
/// Creates a user session
/// </summary>
/// <param name="session"></param>
/// <param name="ct">A token that can be used to request cancellation of the asynchronous operation.</param>
/// <returns></returns>
Task CreateUserSessionAsync(UserSession session, CT ct = default);
/// <summary>
/// Updates a user session
/// </summary>
/// <param name="key"></param>
/// <param name="session"></param>
/// <param name="ct">A token that can be used to request cancellation of the asynchronous operation.</param>
/// <returns></returns>
Task UpdateUserSessionAsync(UserSessionKey key, UserSessionUpdate session, CT ct = default);
/// <summary>
/// Deletes a user session
/// </summary>
/// <param name="key"></param>
/// <param name="ct">A token that can be used to request cancellation of the asynchronous operation.</param>
/// <returns></returns>
Task DeleteUserSessionAsync(UserSessionKey key, CT ct = default);
/// <summary>
/// Queries user sessions based on the filter.
/// </summary>
/// <param name="partitionKey">The partition key to use</param>
/// <param name="filter"></param>
/// <param name="ct">A token that can be used to request cancellation of the asynchronous operation.</param>
/// <returns></returns>
Task<IReadOnlyCollection<UserSession>> GetUserSessionsAsync(PartitionKey partitionKey, UserSessionsFilter filter, CT ct = default);
/// <summary>
/// Deletes user sessions based on the filter.
/// </summary>
/// <param name="partitionKey">The partition key</param>
/// <param name="filter"></param>
/// <param name="ct">A token that can be used to request cancellation of the asynchronous operation.</param>
/// <returns></returns>
Task DeleteUserSessionsAsync(PartitionKey partitionKey, UserSessionsFilter filter, CT ct = default);
}

Once you have an implementation, you can register it when you enable server-side sessions:

Program.cs
builder.Services.AddBff()
.AddServerSideSessions<YourStoreClassName>();

The IUserSessionStoreCleanup interface is used to model cleaning up expired sessions.

/// <summary>
/// User session store cleanup
/// </summary>
public interface IUserSessionStoreCleanup
{
/// <summary>
/// Deletes expired sessions
/// </summary>
Task DeleteExpiredSessionsAsync(CancellationToken cancellationToken = default);
}