Redis Session Provider for Owin.
C# Shell
Latest commit 31558c5 Dec 7, 2013 @neuecc Update


Redis Session Provider for Owin built by CloudStructures which is Redis Client based on BookSleeve.


using with NuGet, Owin.RedisSession

PM> Install-Package Owin.RedisSession


// enable RedisSession Middleware.
app.UseRedisSession(new RedisSessionOptions(new RedisSettings("")));
app.Run(async context => // request begin, middelware get all values from Redis server.
    // take session from owin environment(IDictionary<string, object>)
    var session = context.Environment.AsRedisSession();

    // TryGet(or Get) take from local storage.
    DateTime lastAccess;
    int accessCount = 1;
    if (session.TryGet<DateTime>("LastAccess", out lastAccess) && session.TryGet<int>("Counter", out accessCount))
        await context.Response.WriteAsync("AccessCount " + accessCount);
        await context.Response.WriteAsync(", LastAccess from RedisSession => " + lastAccess.ToString());
        await context.Response.WriteAsync("First Access");

    // Set(or Remove) set to local storage and enqueue operation.
    session.Set("Counter", accessCount);
    session.Set("LastAccess", DateTime.Now);

    context.Response.ContentType = "text/plain";
}); // request end, middleware queued set(or delete) values to Redis server.

Full code is avaliable on this Repositry, Owin.RedisSession.Sample.SelfHost.

with OwinRequestScopeContext

OwinRequestScopeContext enables take RedisSession everywhere. create helper extension methods will be happy.

app.UseRedisSession(new RedisSessionOptions(new RedisSettings("")));
app.Run(async context =>

    int v = -1;
    context.Environment.AsRedisSession().TryGet<int>("test", out v);
    context.Response.ContentType = "text/plain";
    await context.Response.WriteAsync(v.ToString());

void Store()
    // GetSession everywhere.
    var session = OwinRequestScopeContext.Current.Session();
    session.Set<int>("test", new Random().Next());

public static class Extensions
    public static RedisSession Session(this IOwinRequestScopeContext context)
        return context.Environment.AsRedisSession();


2013-12-08 ver 1.0.0

  • first release.


under MIT License