Unique session in multiple browser tabs in ASP.NET MVC

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

I tried to execute below solution

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

However, it does not work in ASP.NET MVC. Above solution using ViewState in MVC, I tried using TempData, but it does not work.

+6
source share
3 answers

Over the years, I finally found a solution to this problem:

1.) The browser SHOULD provide the identifier itself, since http is stateless, cookies are not stored for a separate browser tab, so an asp.net session does not distinguish between separate tabs.

2.) When opening a new tab, the JS window name is ''. After changing the name of window.name to a certain value, it will no longer change until the tab is alive and has been changed by some code.

So, the solution: Embedding a small JS code in a * .master or each * .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 a section section of the * .master file or each * .aspx file:

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

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

+4
source

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

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

2) Do something similar to the one described in the previous paragraph, but not in the same way, and this is what I did to solve the same problem.

a) My links to the serach page is / Product / List? guid = xxx instead of just / Product / List. If the user manually creates / Product / List, I redirect it to the new URL where the GUID is set.

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

Each time you click on the list link and configure a new tab, a new GUID is created.

b) I have a session key with a GUID, so each page has its own values. You can open 2 tabs at the same time, and they are going to use different session values, because guid will be different.

This solution is not perfect, but at least it works.

Hope this helps.

+1
source

A variation on the first answer with HTML 5 is to use window.sessionStorage instead of setting window.name. This way you can do something like this on the app login page:

 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"); 
0
source

All Articles