Red.CookieSessions 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Red.CookieSessions --version 1.0.0                
NuGet\Install-Package Red.CookieSessions -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="Red.CookieSessions" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Red.CookieSessions --version 1.0.0                
#r "nuget: Red.CookieSessions, 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.
// Install Red.CookieSessions as a Cake Addin
#addin nuget:?package=Red.CookieSessions&version=1.0.0

// Install Red.CookieSessions as a Cake Tool
#tool nuget:?package=Red.CookieSessions&version=1.0.0                

Simple session management middleware for RedHttpServer.

Usage

After installing and referencing this library, the Red.Request has the extension methods OpenSession(sessionData) and GetSession().

OpenSession(sessionData) will open a new session and add a header to the response associated with the request.

GetSession() will return then Session object which has two methods: Renew() and Close(), and the field Data, which holds the session-data object

Example

server.Use(new CookieSessions(new CookieSessionSettings(TimeSpan.FromDays(1))
{   // We allow unauthenticated users to send requests to /login, so we can authenticate them
    Excluded = { "/login" }
}));
server.Post("/login", async (req, res) =>
{
    var form = await res.GetFormDataAsync();
    if (ValidForm(form) && Authenticate(form["username"], form["password"]))
    {
        req.OpenSession(form["username"]); // Here we just have the username as session-data
        await res.SendStatus(HttpStatusCode.OK);
    }
    else 
        await res.SendStatus(HttpStatusCode.BadRequest);
});
// Only authenticated users are allowed to /friends
server.Get("/friends", async (req, res) => 
{
    var username = (string) req.GetSession().Data; // We know which user, by looking at session-data
    var friends = database.GetFriendsOfUser(username);
    await res.SendJson(friends);
});
server.Post("/logout", async (req, res) => 
{
    req.GetSession().Close();
    await res.SendStatus(HttpStatusCode.OK);
});
Implementation

OpenSession will open a new session and attach a Set-Cookie header to the associated response. This header's value contains the token used for authentication. The token is generated using the RandomNumberGenerator from System.Security.Cryptography, so it shouldn't be too easy to "guess" other tokens, even with knowledge of some tokens.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 is compatible.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on Red.CookieSessions:

Package Downloads
Red.CookieSessions.EFCore

A EntityFrameworkCore session store for Red.CookieSessions

Red.CookieSessions.LiteDBStore

A LiteDB session store for Red.CookieSessions

Red.CookieSessions.SQLiteStore

A SQLite session store for Red.CookieSessions, to persists sessions

Red.CookieSessions.RedisStore

A Redis session store for Red.CookieSessions

GitHub repositories

This package is not used by any popular GitHub repositories.

First release