10

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.

3
  • Have you tried with a Session? Commented Apr 2, 2014 at 15:49
  • There is no way for your server to know when a user has opened a page in a new tab. (Some browser(s) may have such an extension, but 99.999% of your users will not). Commented Apr 2, 2014 at 15:51
  • It's not working with Session as session values get overwritten. Commented Apr 2, 2014 at 15:59

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm trying to imagine all of the pitfalls in this approach. Could you share whether you still recommend this technique?
And how about MVC??
2

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.

Comments

0

A variation of the first answer, with HTML 5, is to use window.sessionStorage instead of setting window.name. So you could do something like this on the entry page of your application:

var uniqueId = "<%= Guid.NewGuid().ToString() %>";
window.sessionStorage.setItem("tabId", uniqueId);

Then in every controller action that requires a session you pass the parameter with:

window.sessionStorage.getItem("tabId");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.