Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unique session in multiple browser tabs in ASP.NET MVC

I want to create unique session whenever user open a new browser tab or window in ASP.NET MVC application. Is it possible in ASP.NET / MVC ?

I tried to follow below solution

asp.net - session - multiple browser tabs - different sessions?

However it doesn't work in ASP.NET MVC. Above solution using ViewState, in MVC I tried using TempData but it doesn't work.

like image 475
Pinal Dave Avatar asked Apr 02 '14 15:04

Pinal Dave


2 Answers

After years I finally found the solution for this Problem:

1.) The browser MUST provide the ID by himself, since http is stateless, cookies are stored not for the browser tab seperately, and therefore the asp.net session does not distinguish the single tabs as well.

2.) When opening a new tab, the JS window.name is ''. After changing the window.name to some value, it will not change anymore as long as the tab is alive and it is not changed by some code.

So the solution is: Implement some short JS code in the *.master or every *.aspx file:

 if (typeof window.name != undefined) {
             if (window.name == '') {
                 var d = new Date();
                 window.name = '_myWnd_' + d.getUTCHours() + d.getUTCMinutes() + d.getUTCSeconds() + d.getUTCMilliseconds();
             }
             var eDiv = document.getElementById('div_BrowserWindowName');
             var e = eDiv.getElementsByTagName('input')[0];
             e.value = window.name;
         }

Also add in the form section of the *.master or every *.aspx file:

 <div id="div_BrowserWindowName" style="visibility:hidden;">
                <asp:HiddenField ID="hf_BrowserWindowName" runat="server" />
            </div>

Now you can retrieve the window.name == UNIQUE ID by reading the hidden field after every postback, even if the user jumps from site to site and returns back after hours to the form.

like image 69
user4303956 Avatar answered Oct 03 '22 23:10

user4303956


As you know, HTTP is stateless and a very common mechanism to have state between requests is use session variables. The problem occurs when you open a new brower tab because the session is the same so any change you make in the new tab is gonna impact other tabs. You didn't specify exactly what you want to do but let's say that you have a product list page where the user can enter search filters and you want to save them in the session. If the user sets a search filter value in tab 1, tab 2 is gonna have the same value (they share session variables). What you can do?

1) Use this approach for adding a guid in the URL: http://www.codeproject.com/Articles/331609/Get-an-unique-session-in-each-browser-tab

2) Do something similar to what's described in the previous point but not in the same way and this is what I did to solve the same problem.

a) My links to the serach page are /Product/List?guid=xxx instead of just /Product/List. If the user manually types /Product/List, I'm redirecting him to a new URL where the GUID is set.

public ActionResult List(string guid)
        {
            if (guid == null)
            {
                return RedirectToAction("List", new { guid = Guid.NewGuid().ToString() });
            }
...

Every time you click on the "list" link and target a new tab, a new GUID is generated.

b) I have a session key with the GUID so each page has its own values. You can have 2 tabs opened at the same time and they are gonna be using different session values because the guid is gonna be different.

This solution is not perfect but at least it works.

I hope it helps.

like image 22
Francisco Goldenstein Avatar answered Oct 03 '22 21:10

Francisco Goldenstein