BlazorSessionProvider 1.0.1
dotnet add package BlazorSessionProvider --version 1.0.1
NuGet\Install-Package BlazorSessionProvider -Version 1.0.1
<PackageReference Include="BlazorSessionProvider" Version="1.0.1" />
paket add BlazorSessionProvider --version 1.0.1
#r "nuget: BlazorSessionProvider, 1.0.1"
// 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
- Install the package via NuGet:
dotnet add package BlazorSessionProvider --version 1.0.0
- 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.
- 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
- 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.
- 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);
- 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 | Versions 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. |
-
net8.0
- Microsoft.AspNetCore.Components.Web (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Released v1.0.1