简体   繁体   中英

asp.net mvc Azure cloud services, should I use TempData or Sessions to hold some user info or something else

I am developping a website that will be hosted on azure cloud services. I am wondering about the best way to save some information of the logged user (basically some Ids, and a list/dictionary).

The app could be deployed on multiple instances. I know for sure that static variables are not a solution.

So is it safe to use Sessions, should I go with TempData (using the peek method or just making a wrapper class that will use the peek method) or something else?

Basically I would like to avoid calls to the database to retrieve same data because itself is reached through a webservice.

data stored in TempData will be cleared in the next request, it's just a temporary store (eg when you want to maintain data when doing redirect), so it's not a good place to put data that is valid for the whole session

Normally user information is stored in 2 ways after successfully log in:

  • Using session: it's safe because the data is stored on server (if using in-proc session) or on another sever (if using distributed session). For cloud, you should use Azure cache service or Azure Redis as session provider
  • If amount of user information is small, you can serialize it and put in authentication cookie. At each request, client will send cookie to server, server will deserialize the data into custom principal and store as current user. The disadvantage is that size of cookie will be larger compared to first approach, so only use it when the amount of information is small. And it's also safe because cookie will be encrypted

TempData only stores data the time of your HTTP request. Session is not very reliable, and is memory consuming, as long you don't have a shared Session provider, like a SQLSessionProvider.

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