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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Shaunebu.MAUI.SessionManager" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Shaunebu.MAUI.SessionManager" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Shaunebu.MAUI.SessionManager" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Shaunebu.MAUI.SessionManager --version 1.0.0
                    
#r "nuget: Shaunebu.MAUI.SessionManager, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Shaunebu.MAUI.SessionManager@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Shaunebu.MAUI.SessionManager&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Shaunebu.MAUI.SessionManager&version=1.0.0
                    
Install as a Cake Tool

Shaunebu.MAUI.SessionManagerπŸ“˜

NuGet Version

NET Support NET Support NET Support Support

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 MainActivity override).

  • βœ… 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 SessionAlert to warn users before expiration.

  • Use SessionExpired to force logout or lock screen.

  • Always implement the Android OnUserInteraction override if you need automatic extension.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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