jaxmeier
jaxmeier

Reputation: 509

Storing an ASP.NET Session in Redis and reading it in ServiceStack

All,

We have several ASP.NET websites that are using the RedisSessionStateProvider for session storage. We are just starting to spin up an instance of ServiceStack, and I would like to pass the sessionId from ASP.NET to ServiceStack, then use the stored values from Redis in ServiceStack sessions.

So far I have been able to pass the sessionID in the header, retrieve it in my plugin, and get the value matching that sessionId from Redis back as a string.

My problem is is a typed object going into Redis from ASP.NET, but I can't get it as the type object coming out in ServiceStack.

Any and all suggestions are appreciated.

Thanks, B

Upvotes: 3

Views: 1266

Answers (1)

mythz
mythz

Reputation: 143399

ServiceStack Sessions are completely decoupled/independent from ASP.NET Sessions and its Session Provider model. As they're 2 completely different technologies they're incompatible with each other, you'll need a separate "migration step" to extract out data from ASP.NET Session State, populate your Typed Custom UserSession and save it in ServiceStack.

ServiceStack Sessions are simply your Typed UserSession persisted in the registered Caching Provider at a key identified from the Cookie SessionId.

Navigating all User Sessions in Cache

The Inspecting Persisted User Sessions illustrates how User Sessions are serialized POCO's stored in the Registered ICacheClient at the following key:

urn:iauthsession:{SessionId}

Where {SessionId} is either the users ss-id or ss-pid cookie depending on whether the user was authenticated with RememberMe=true which instructs ServiceStack to save the session against the ss-pid permanent cookie - this preference is stored in the ss-opt=perm cookie.

Since they're just plain POCO's stored at a predictable key format, we can easily iterate through all user sessions by using the ICacheClient API's directly, e.g:

var sessionPattern = IdUtils.CreateUrn<IAuthSession>(""); //= urn:iauthsession:
var sessionKeys = Cache.GetKeysStartingWith(sessionPattern).ToList();

var allSessions = Cache.GetAll<IAuthSession>(sessionKeys);

Upvotes: 1

Related Questions