"Static" data per user in ASP.Net MVC application

I created a "singleton" object in a base controller and it is used for several parts in my Home controller actions.
private Participant _Part;
public Participant Part
{
    get 
    {
        if(_Part == null) {
            // LT and PID are stored in Session variables when user logs in
            _Part = mps.GetPart(LT, PID);
        }
        return _Part;
    }
    set
    {
        _Part = value;
    }
}

Open in new window

Used in Home Controller:
public ActionResult MyInfo()
{
    ViewBag.part = Part;
    return view();
}

Open in new window

An in my View:

    <input type="hidden" name="Desig" id="Desig" value="@ViewBag.part.Desig" />
    <input type="hidden" name="Discount" id="Discount" value="@ViewBag.part.Discount" />


Now, If I login as one person, the session variables are set.
Then I open another browser and login as a different user, I see the same data in the other browser.

How do I prevent this and keep the data 'static" for each user?

Do I need to store it in a session variable? is it capable of being store in a session variable?
LVL 27
Eddie ShipmanAll-around developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Najam UddinCommented:
Well, looking at code it should not behave like static. Are you sure you didn't use static keyword in class, ie your class definition is as is you pasted above? As every user request will have separate instance.
Eddie ShipmanAll-around developerAuthor Commented:
Well, it acts more like a singleton than a static. However it seems that it is shared for the entire app vs. between sessions.

I saw behavior where a user logging in saw the same data as someone that was already logged in so I do not believe each request has a different instance.

I rewrote it to store to session but it also shares between tabs in same browser. It does work for different browsers, however:
private Participant _Part;
public Participant Part
{
    get
    {
        System.Web.HttpSessionStateBase Sess = HttpContext.Session;
        if ((Participant)Sess["PartData"] == null)
        {
            // LT and PID are stored in Session variables when user logs in
            _Part = mps.GetPart(LT, PID);
            HttpContext.Session.Add("PartData", _Part);
        }
        else
        {
            _Part = (Participant)Sess["PartData"];
        }
        return _Part;
    }
    set
    {
        _Part = value;
    }
}

Open in new window

Najam UddinCommented:
Between tabs, I can understand as they share same session, until you explicitly open a tab as new session.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

käµfm³d 👽Commented:
A session cookie is stored per browser session on the client. You're not going to be able to log in as a different user using the same browser while the existing login is open--even if you open a new tab. If you want to test something like this, then use a different browser simultaneously--e.g.  one FireFox, one Chrome.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eddie ShipmanAll-around developerAuthor Commented:
Has anyone seen methods of caching data like this for ASP.Net MVC applications? I need to cache PER USER, not necessarily per tab.
käµfm³d 👽Commented:
Why would two users be on the same machine?
Eddie ShipmanAll-around developerAuthor Commented:
Our phone operators may need to open 2 user accounts on same machine.
käµfm³d 👽Commented:
Point taken. I'll have to bow out, then. I believe there are some hacky ways to accomplish this, but I haven't personally done any of them. You're basically fighting the browser at this point.
Eddie ShipmanAll-around developerAuthor Commented:
Do you think something like CacheCow would work for this?
Najam UddinCommented:
Try cookie less sessions, your session key will be in url so you can safely have 2 different tab with different session.
Eddie ShipmanAll-around developerAuthor Commented:
@Najim, How will that work in ASP.Net MVC?
Najam UddinCommented:
Wont work in MVC, newer tech from microsoft don't support cookie less.

 However there is another way, but not 100% sure

1. Create a random identifier and store in hidden field

//in pageload
  if(!IsPostback)
   //generate random unique identifier

and store session key with this identifier

Session(uniqueIdentifier + session key) = value.

Open in new window


It will be different for each page rendered, so different tab will have actually different values from session
Najam UddinCommented:
Also if you are using HTML5 then sessionStorage will help

Data stored using sessionStorage do not persist across browser tabs, even if two tabs both contain webpages from the same domain origin. In other words, data inside sessionStorage is confined to not just the domain and directory of the invoking page, but the browser tab in which the page is contained in. Contrast that to session cookies, which do persist data from tab to tab.
käµfm³d 👽Commented:
@Najam

I believe you'd lose a user's session--effectively--if they navigated directly to a page (via the address bar) using that approach (in http:#a41241266). They'd still have an open session, but the unique ID would be different, so you wouldn't be able to get the original values.
Najam UddinCommented:
@käµfm³d 👽
If request is send back to server with page id (unique identifier) they should be getting saved value. Yes you are right that getting this to work in other pages will be complicated.

I am trying to think of any kind of tweak that can work, Like for sessionStorage, this can be base to such solution. One can generate a unique id, store in sessionStorage and other tab will not have this value so it will generate its own unique id. Session will be same for tabs, but by appending unique id we can different value set for different tabs
Eddie ShipmanAll-around developerAuthor Commented:
@Najam
LocalStorage won't help, either because we have many IE users with versions < IE10.
Besides, one other thing we need, a list of documents, may be thousands of records.
Najam UddinCommented:
Then only option I see is playing around previous solution, although as pointed by @käµfm³d 👽 this will be  complex thing to do
Eddie ShipmanAll-around developerAuthor Commented:
May have to go another route to get this solved.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.