简体   繁体   中英

ASP.NET cookie-based sessions, is there SessionStateModule for it?

I am looking for a way to do fully cookie-based sessions in ASP.NET. I don't intend to store complex objects in them, just string key-value pairs.

I am looking for something similar to how the Play! framework handles sessions, it basically encodes all the session data in a cookie and takes care of encrypting/decrypting it on each request.

I don't want to have to write my own SessionStateModule for this if I can avoid it, does one exist?

Thanks

EDIT: I don't need to store more data than a cookie can handle (just a few identifiers) and I still need the key-value pairs to be specific to each session. I would like to use cookies so that no server state has to be maintained (either in process or in a database) which allows me to add more servers quite easily and also depend less on external applications (no state server/memcached/redis etc)

You won't be able to store that much data in a cookie unless you are going to create lots of them.

Given you want to work with key value pairs have considered using a cache such as memcached or redis. Or even the server cache if you are not in a web farm?

I don't think one exists: it would be too fragile because of the restrictions inherent in using cookies (notably the maximum size).

And I know this isn't answering your question, but I wouldn't implement a SessionStateModule for this. Doing so would mean that any 3rd party components that use Session, or any future maintenance programmers who use Session would possibly be storing inappropriate data (eg too large) in the cookie.

I'd suggest you write your own API oriented round storing key-value pairs with only primitive values (string, Int32, DateTime,...) in your cookie.

You can also make the API "cookie-aware" - eg set HttpCookie.Path which gives you the possibility to:

  • avoid sending cookies for requests that don't need them (javascript, images, ...)

  • have different cookies (same name but different Path) for different Areas of your application. This can help keep the cookie size down.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM