The Nancy project is no longer active; this project has followed its lead.
This is a community project that provides several session store implementations for Nancy, the lean-and-mean web framework that runs under the .NET framework. It also provides extensions to the Nancy Request
object that allow strongly-typed retrieval of objects from the session. It uses cookies to associate the session Id with a request.
The Nancy.Session.Persistable
pacakge will be installed when you install any of the available back-end storage packages. Currently available:
Data Store | Docs | Package | Stable (Nancy v2) |
---|---|---|---|
Relational | Wiki | NuGet | |
RavenDB | Wiki | NuGet | |
RethinkDB | Wiki | NuGet | |
MongoDB | Wiki | NuGet | |
In Memory | Wiki | NuGet |
NOTE: While the API is currently thought stable, it may change up until a 1.0 release.
NOTE 2: Possible future implementations include Redis and disk storage.
To enable sessions, you have to override the default Nancy bootstrapper. This sounds way scarier than it actually is; you can do it in just a few lines of code. Following their lead, persistable sessions are fully SDHP-compliant.
You can do it in C#...
namespace ExampleNancyApp
{
using Nancy;
using Nancy.Bootstrapper;
using Nancy.Session.Persistable;
using Nancy.TinyIoc;
public class ApplicationBootstrapper : DefaultNancyBootstrapper
{
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
base.ApplicationStartup(container, pipelines);
// Enable sessions
PersistableSessions.Enable(pipelines, [config]);
}
}
}
... or F# ...
module ExampleNancyApp
open Nancy
open Nancy.Bootstrapper
open Nancy.Session.Persistable
type ApplicationBootstrapper() =
inherit DefaultNancyBootstrapper()
override this.ApplicationStartup (container, pipelines) =
base.ApplicationStartup (container, pipelines)
// Enable sessions
PersistableSessions.Enable (pipelines, [config])
... or even Visual Basic.NET!
Imports Nancy
Imports Nancy.Bootstrapper
Imports Nancy.Session.Persistable
Imports Nancy.TinyIoc
Namespace ExampleNancyApp
Public Class ApplicationBootstrapper
Inherits DefaultNancyBootstrapper
Protected Overrides Sub ApplicationStartup(container As TinyIoCContainer, pipelines As IPipelines)
MyBase.ApplicationStartup(container, pipelines)
' Enable sessions
PersistableSessions.Enable(pipelines, [config])
End Sub
End Class
End Namespace
The store-specific [config]
object is detailed in each implementation project. Each of these objects takes an optional Nancy.Cryptography.CryptographyConfiguration
parameter that is used to control the cryptography used for the session Id cookie. If it is not specified, it uses a default configuration.
Retrieving the current session needs to happen early in the request cycle, and persisting it needs to happen as late as possible. So, in your bootstrapper, put the PersistableSessions.Enable
call as late as possible.
Common and implementation-specific configuration options and usage can be found in the wiki.