Alga.sessions 1.1.3

dotnet add package Alga.sessions --version 1.1.3
                    
NuGet\Install-Package Alga.sessions -Version 1.1.3
                    
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="Alga.sessions" Version="1.1.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Alga.sessions" Version="1.1.3" />
                    
Directory.Packages.props
<PackageReference Include="Alga.sessions" />
                    
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 Alga.sessions --version 1.1.3
                    
#r "nuget: Alga.sessions, 1.1.3"
                    
#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 Alga.sessions@1.1.3
                    
#: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=Alga.sessions&version=1.1.3
                    
Install as a Cake Addin
#tool nuget:?package=Alga.sessions&version=1.1.3
                    
Install as a Cake Tool

Alga.sessions

A lightweight .NET library for streamlined session management: Create, Refresh, Validation, Deletion. Sessions are stored in RAM for quick access. For long-term storage of sessions, you can use an automatically created file that is updated once a minute, for this you just need to specify the path to the directory.

How does this work. Step by step

  1. Install-Package Alga.sessions

  2. Setting up the configuration (appsettings.json)

{
    ...,
    "AlgaSessionsConfig": {
        "SessionIdLength": 32, 
        "SessionTokenLength": 128,
        "SessionRefreshIntervalInMin": 5 
        "SessionLifetimeInMin": 5040,
        "SessionMaxNumberOfErrors": 10000000,
        "StorageDirectoryPath": "C:\\",
        "StorageEncryptionKey": "aA1bB2cC3dD4efE5gG6hH7"
    }
}

SessionIdLength - Session's id length. Default is 32 SessionTokenLength - Session's token length. Default is 128 SessionRefreshIntervalInMin - Interval in minutes at which the session is proactively refreshed to extend its lifetime. This does not affect the maximum session lifetime. Default is 0 minutes. SessionLifetimeInMin - Session's life time in min, if there was no refresh. Default is 10080 (7 day) SessionMaxNumberOfErrors - Max error number. If the number of variables under the current key exceeds this number, the session will be deleted from memory immediately StorageDirectoryPath - Path to the folder where your sessions will be stored for a long time. Optional parameter StorageEncryptionKey - Key for encrypting data in storage file. Optional parameter

  1. Registers sessions provider as a singleton with config from 'AlgaSessionsConfig'.
var algaSessionsConfig = builder.Configuration.GetSection("AlgaSessionsConfig").Get<Alga.sessions.Models.Config>();
builder.Services.AddSingleton(sp => new Alga.sessions.Provider(algaSessionsConfig));
  1. Create session (server code)

Storing sessions in memory is an expensive operation, so it is recommended to create a session only if the user is authenticated by: Login/Password or OAuth: Google / Facebook/ X /...

After authentication, it is necessary to define the data model that will be transferred to the client, they can be anything, a data: role, user id, etc.

var userContext = new { userId = m.userId, roleId = m.roleId }; 

Create session:

var session = sessionProvider.Create(JsonSerializer.Serialize(userContext));
  1. Refresh session

The client sends the user's data model to the server that it received before. The session token (getted form user's data model) is checked and the data model with the new key is returned to the client.

Client:

var headers = { };
headers["AlgaSession"] = localStorage.getItem("AlgaSession");
const response = await fetch($"/SessionRefresh", { "POST", headers });

if (response?.ok && response.status === 200) {
    var json = await response.json();

    if (json?.roleId) {
        localStorage.setItem("AlgaSession", JSON.stringify(json));
    } else localStorage.removeItem("AlgaSession");
}

Server:

app.MapGet($"/SessionRefresh", (HttpContext context, Alga.sessions.Simple sessionProvider) => { 
    var head = Context.Request.Headers["AlgaSession"].ToString();
    var session = sessionProvider.Refresh(head);
    return Results.Text(session);
});

  1. Check session
Microsoft.Extensions.Primitives.StringValues value = ""; 
context.Request.Headers.TryGetValue(name, out value);
if (session.Check(value))
    return true;
  1. Delete session
app.MapPost($"{UR_Auth}/Signout", (HttpContext context, Alga.sessions.Simple sessionProvider) => { 
    var head = Context.Request.Headers["AlgaSession"].ToString();
    var idF = sessionProvider.Delete(head);
    if (!idF) return Results.BadRequest();
    return Results.Ok();
});

Upates

What has been changed in new build (1.1.3) compared to the previous version (1.1.1)

  • Critical error fixed
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos 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.
  • net9.0

    • No dependencies.

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.1.3 154 6/5/2025
1.1.1 243 6/4/2025 1.1.1 is deprecated because it has critical bugs.
1.0.1 155 5/27/2025
1.0.0 152 5/27/2025