Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing SessionID Dynamically in C# ASP.NET

I'm curious if there is a way to dynamically change the session ID from the context of a SessionStateStoreProvider (or some other customizable module) in C# for ASP.NET.

I am implementing a custom SessionStateStoreProvider, and I was thinking about augmenting the session ID to tell the store provider where to go look for a session. I've implemented a custom SessionIDManager, which lets me augment newly created session IDs with the required tag. The problem is that the desired value of that tag might change during the life of a session. For example, the session may be read from one location, but may need to written to a different location. In this instance the ID would have originally been tagged for location A, but on writing the store would want to write to location B. The tag should be updated to reflect location B for the next session read.

So, from the context of SessionStateProviderBase override...

public override void SetAndReleaseItemExclusive(HttpContext context, 
    string id, SessionStateStoreData item, object lockId, bool newItem)

... is it possible to change the session ID? I know that the HttpContext.Session.SessionID property is not settable. Is there some other way to feedback an update like this? Or is there a more appropriate to carry state like this across calls?

I'm new to C# and web development in general, so any suggestions would be appreciated.

like image 221
Adam Holmberg Avatar asked Jul 16 '26 13:07

Adam Holmberg


1 Answers

After asking this question, I became reasonably certain that it is not possible to change the Session ID dynamically.

Instead of tagging the session ID with the location information, I ended up setting a cookie using the HttpContext passed into the SessionStateStoreProvider interfaces:

context.Response.SetCookie(new HttpCookie(...));
like image 78
Adam Holmberg Avatar answered Jul 18 '26 04:07

Adam Holmberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!