BlazorSessionProvider 1.0.1

dotnet add package BlazorSessionProvider --version 1.0.1                
NuGet\Install-Package BlazorSessionProvider -Version 1.0.1                
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="BlazorSessionProvider" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BlazorSessionProvider --version 1.0.1                
#r "nuget: BlazorSessionProvider, 1.0.1"                
#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.
// Install BlazorSessionProvider as a Cake Addin
#addin nuget:?package=BlazorSessionProvider&version=1.0.1

// Install BlazorSessionProvider as a Cake Tool
#tool nuget:?package=BlazorSessionProvider&version=1.0.1                

BlazorSessionProvider

Introduction

BlazorSessionProvider is a Blazor Server library that handle sessions inside the application. Simple to install and configure.

Requirements

  • This library works under .NET 8 and higher

Quick Install

  1. Install the package via NuGet:
dotnet add package BlazorSessionProvider --version 1.0.0
  1. Add the next code line in your Blazor project, on Program.cs:
using BlazorSessionProvider;
var builder = WebApplication.CreateBuilder(args);
...
builder.Services.AddSessionProvider(config => {
    config.TimeDelay           = new TimeSpan(0, 0, 30);
    config.SessionExpiredUrl   = "/logout";
    config.SessionNotFoundUrl  = "/";
});

Where:

  • config.TimeDelay is the time it will take for a session to expire from when it is initialized within the application.
  • config.SessionExpiredUrl is the URL of the application to which it will redirect when the session has expired.
  • config.SessionNotFoundUrl is the URL of the application to which it will redirect when the application does not find the session key.
  1. Add these lines in your _Imports.razor file:
@using BlazorSessionProvider.Sessions
@inject ISessionProvider SESS

Make sure to put the render mode in "InteractiveServer" on each page you want to manage your servers.

@rendermode InteractiveServer

Quick Usage

  1. To store a session, add this line in your page/component:
SESS.CreateNewSession(new KeyValuePair("Key", Value));

You can use the same line to replace the value for "Key" session.

  1. To get it, use this line in your page/component:
var mySession = await SESS.GetSession<object>("Key");

If you want to delete the session, just add true in the removeIt property:

var mySession = await SESS.GetSession<object>("Key", true);
  1. If you want to delete the actual session manually, use:
SESS.RemoveSession();

This will NOT delete all the sessions stored in the application.

You can't manage sessions in the method OnInitialized or OnInitializedAsync, because BlazorSessionProvider use JavaScriptInterop to get the key session in the browser.

protected override async Task OnInitializedAsync()
{
	string errorSess = await SESS.GetSession<string>("your_session_name");
}

/*
	This will throw the next exception:
	InvalidOperationException: JavaScript interop calls cannot be issued at this time...
*/

Manage your sessions inside (or after) OnAfterRender or OnAfterRenderAsync to avoid this error

protected override async Task OnAfterRenderAsync(bool firstRender)
{
	...
	string goodSess = await SESS.GetSession<string>("your_session_name");
	...
}

Docs

For more information, see the Wikia.


Thank you so much for using BlazorSessionProvider.

Made with ❤️ by Oscar D. Soto

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.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.1 95 10/20/2024
1.0.0 145 1/25/2024

Released v1.0.1