Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is storing values in TempData safe for multiple concurrent users?

I've been using TempData to preserve my model in between page requests, as otherwise I lose access to specific properties of classes in the model that I need.

(It's a slight digression and simplification, but basically I have a List of objects which have a .name and a .active property. I have a CheckBoxFor the .active property, and that is preserved, but the .name property is set to null when the form is submitted; I need access to that property as well, so I've been storing the old model in TempData and then copying over that property when I need it. Is there a better way of doing that? I was quite surprised that properties of objects that I'm using would be nulled.)

The model is stored in TempData when the user checks a checkbox, thus submitting the form. It is read out of TempData at some later time, when the user clicks a button. (There are no intervening requests.)

Is using TempData in such a way safe for multiple users? In other words, does each client get its own copy of TempData? I'm worried about a situation roughly like the following:

  1. User 1 clicks checkbox, saving his model to TempData
  2. User 2 clicks checkbox, saving her model to TempData
  3. User 1 clicks button, submitting the form and reading a model from TempData

What I am unsure about is which model User 1 will get. Could someone please enilghten me?

like image 772
sapi Avatar asked Dec 29 '11 01:12

sapi


People also ask

Is TempData safe to use?

Yes, TempData is backed by session storage, so if you are in a load balanced environment extra care must be taken when using it (sticky sessions, persistent session state, etc). TempData has been the de-facto choice when using the PRG pattern, and is what it was designed for.

Does TempData preserve data in the next request also?

Tempdata helps to preserve values for a single request and CAN ALSO preserve values for the next request depending on 4 conditions”. You can use Peek when you always want to retain the value for another request.

Is TempData private to a user?

Yes, Tempdata is private to a user.


1 Answers

TempData is session bound. User 1 will only see its TempData.

like image 85
Daniel A. White Avatar answered Sep 27 '22 18:09

Daniel A. White