Shaunebu.MAUI.SessionManager
1.0.0
dotnet add package Shaunebu.MAUI.SessionManager --version 1.0.0
NuGet\Install-Package Shaunebu.MAUI.SessionManager -Version 1.0.0
<PackageReference Include="Shaunebu.MAUI.SessionManager" Version="1.0.0" />
<PackageVersion Include="Shaunebu.MAUI.SessionManager" Version="1.0.0" />
<PackageReference Include="Shaunebu.MAUI.SessionManager" />
paket add Shaunebu.MAUI.SessionManager --version 1.0.0
#r "nuget: Shaunebu.MAUI.SessionManager, 1.0.0"
#:package Shaunebu.MAUI.SessionManager@1.0.0
#addin nuget:?package=Shaunebu.MAUI.SessionManager&version=1.0.0
#tool nuget:?package=Shaunebu.MAUI.SessionManager&version=1.0.0
Shaunebu.MAUI.SessionManagerπ
Overview
Shaunebu.MAUI.SessionManager is a lightweight session management utility for .NET MAUI applications.
It provides automatic session tracking, expiration events, and alert notifications before timeout, making it ideal for apps that require login sessions, inactivity timeouts, or custom session handling.
β¨ Features
π Session start, extension, and expiration handling.
β³ Configurable session duration.
β οΈ Configurable alert trigger before expiration.
π‘ Event-based API (
SessionExpired,SessionAlert) using WeakEventManager (prevents memory leaks).π± Cross-platform (iOS auto-wired, Android requires a small
MainActivityoverride).β Simple integration into any MAUI project.
π Getting Started
1. Install the Package
Add the library reference to your project:
dotnet add package Shaunebu.MAUI.SessionManager
2. Starting a Session
// Start a new session with default duration
await SessionManager.Instance.StartTrackSessionAsync();
By default, the session lasts ~1.3 minutes (for demo purposes). You can change this:
SessionManager.Instance.SessionDuration = TimeSpan.FromMinutes(10); // total duration
SessionManager.Instance.AlertBeforeExpiration = TimeSpan.FromMinutes(2); // alert before expiration
3. Subscribing to Events
You can subscribe to SessionAlert and SessionExpired events anywhere in your app (e.g., in AppShell, a Page, or a ViewModel):
SessionManager.Instance.SessionAlert += (s, e) =>
{
// Show warning to the user (e.g., popup, toast, dialog)
Console.WriteLine("β οΈ Session will expire soon!");
};
SessionManager.Instance.SessionExpired += (s, e) =>
{
// Redirect to login or lock screen
Console.WriteLine("βΉοΈ Session expired!");
};
4. Extending or Ending the Session
- Extend the session manually (e.g., on login success, user interaction, API call):
SessionManager.Instance.ExtendSession();
- End the session manually:
SessionManager.Instance.EndTrackSession();
π± Platform Notes
β iOS
On iOS, the session is automatically extended via gesture recognizers attached to the root window. No extra configuration required.
β οΈ Android
On Android, SessionManager cannot intercept user interactions automatically because OnUserInteraction is part of MainActivity.
To extend sessions when the user interacts with the UI, add the following override in your MainActivity.cs:
public override void OnUserInteraction()
{
base.OnUserInteraction();
SessionManager.Instance.ExtendSession();
}
This ensures that any tap, swipe, or interaction resets the session timer.
π§ Example: Using in a Login Page
private async void OnLoginSuccess()
{
// Configure session
SessionManager.Instance.SessionDuration = TimeSpan.FromMinutes(30);
SessionManager.Instance.AlertBeforeExpiration = TimeSpan.FromMinutes(5);
// Subscribe to session events
SessionManager.Instance.SessionAlert += async (s, e) =>
{
await DisplayAlert("Warning", "Your session will expire in 5 minutes.", "OK");
};
SessionManager.Instance.SessionExpired += async (s, e) =>
{
await DisplayAlert("Session Expired", "Please log in again.", "OK");
await Shell.Current.GoToAsync("//LoginPage");
};
// Start tracking session
await SessionManager.Instance.StartTrackSessionAsync();
}
π API Reference
Properties
SessionDuration : TimeSpanβ Total length of the session.AlertBeforeExpiration : TimeSpanβ Time before expiration to trigger an alert.IsSessionActive : boolβ Indicates if a session is running.HasShownAlert : boolβ Whether the alert event has been triggered.
Events
SessionExpired : EventHandlerβ Raised when the session expires.SessionAlert : EventHandlerβ Raised when the alert time is reached.
Methods
Task StartTrackSessionAsync()β Starts session tracking asynchronously.void EndTrackSession()β Stops session tracking.void ExtendSession()β Extends the session expiration from now.
β Best Practices
Start session after successful authentication.
Use
SessionAlertto warn users before expiration.Use
SessionExpiredto force logout or lock screen.Always implement the Android
OnUserInteractionoverride if you need automatic extension.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0-android35.0 is compatible. net9.0-ios18.0 is compatible. net9.0-maccatalyst18.0 is compatible. net9.0-windows10.0.19041 is compatible. net10.0-android was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-windows was computed. |
-
net9.0-android35.0
- Microsoft.Maui.Controls (>= 9.0.82)
-
net9.0-ios18.0
- Microsoft.Maui.Controls (>= 9.0.82)
-
net9.0-maccatalyst18.0
- Microsoft.Maui.Controls (>= 9.0.82)
-
net9.0-windows10.0.19041
- Microsoft.Maui.Controls (>= 9.0.82)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 331 | 10/3/2025 |