Aspnetcore: SessionIDManager?

Created on 28 Jul 2016  路  8Comments  路  Source: dotnet/aspnetcore

what can I do to generate SessionID by my rule in asp.net core?

Most helpful comment

@Tratcher thanks for your answer!

Is what you said that I should implement a SessionStore to force tryEstablishSession to be () => true; when I create Session instance? Maybe it can be done just by replacing the

services.AddTransient<ISessionStore, DistributedSessionStore>();

by

services.AddTransient<ISessionStore, MyDistributedSessionStore>();

?so why I need middleware here?

I've seen the source of DistributedSession, DistributedSessionStore, and SessionMiddleware, but I 'm not sure I got what tryEstablishSession means... It seems like tryEstablishSession can not return false at all (or there will be an exception throwed): https://github.com/aspnet/Session/blob/dev/src/Microsoft.AspNetCore.Session/DistributedSession.cs#L142, so what am I supposed to do with tryEstablishSession in right way?

However, if I create DistributedSession instance by myself, may I simply set sessionKey by myself to do the same things as SessionIDManager ?

Maybe something I missed, please help me, I really appriciate it !

All 8 comments

I don't think this is possible. Why do you need to generate the session id yourself?

/cc @Tratcher

@davidfowl thanks for your attention !

We have a webapi project(gateway), which is the middle layer between our internal webservices and mobile apps. I've defined some common request params like:

public class CommonParam {

    //mobile app generate a unique token 
    //and use the token in every requests until the app is closed
    public string Token {get;set;}

    //others....
}

for convenience, I want to use the session feature to maintain user(caller)'s state(eg. login), so the only thing I have to do is generate SessionID by the token param.

In simple terms, I just want to use Session["cart"] instead of something like SessionPool[key]["cart"], key is generated by token.

Thanks.

No, half of session's job is to manage the cookie. I think the only part of the middleware you want is the storage provider, which you could new up directly: https://github.com/aspnet/Session/blob/dev/src/Microsoft.AspNetCore.Session/DistributedSession.cs
Just set tryEstablishSession to () => true;

@Tratcher thanks for your answer!

Is what you said that I should implement a SessionStore to force tryEstablishSession to be () => true; when I create Session instance? Maybe it can be done just by replacing the

services.AddTransient<ISessionStore, DistributedSessionStore>();

by

services.AddTransient<ISessionStore, MyDistributedSessionStore>();

?so why I need middleware here?

I've seen the source of DistributedSession, DistributedSessionStore, and SessionMiddleware, but I 'm not sure I got what tryEstablishSession means... It seems like tryEstablishSession can not return false at all (or there will be an exception throwed): https://github.com/aspnet/Session/blob/dev/src/Microsoft.AspNetCore.Session/DistributedSession.cs#L142, so what am I supposed to do with tryEstablishSession in right way?

However, if I create DistributedSession instance by myself, may I simply set sessionKey by myself to do the same things as SessionIDManager ?

Maybe something I missed, please help me, I really appriciate it !

Just new up DistributedSession yourself and ignore the middleware for your scenario. DistributedSesssion implements ISession and should give you what you need. You may need to fake some of the parameters that don't apply to your scenario, like tryEstablishSession (which is normally used to create the cookie for new sessions).

@Tratcher okay, I'll try it out, thanks a lot!

In addition, why the SessionIDManager was gone in asp.net core ? It's convenient to achieve my scenario case by using it.

In addition, why the SessionIDManager was gone in asp.net core ? It's convenient to achieve my scenario case by using it.

Lots of things are gone in ASP.NET Core as it's not a direct port of ASP.NET. The session implementation is also completely different.

get it. thanks a lot

Was this page helpful?
0 / 5 - 0 ratings