Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way around ASP.NET session being shared across multiple tab windows

Tags:

.net

asp.net

I'm storing some value in an asp.net session on the first page. On the next page, this session value is being read. However if multiple tabs are opened and there are multiple page 1->page 2 navigation going on, the value stored in session gets mixed up since the session is shared between the browser tabs.

I'm wondering what are the options around this :

  1. Query String: Passing value between the pages using query string, I don't want to take this approach since there can be multiple anchor tags on page 1 linking to page 2 and I can not rewrite the URLs of each tag since they are dynamic.

  2. Cookies??? In-memory cookies are shared across browser tabs too, same as the session cookie, rite ?

Any other option?

PS: Page 1 to page 2 is not a form submit.

like image 727
ace Avatar asked May 13 '10 18:05

ace


People also ask

Are session variables shared between tabs?

When you open the same screens in different browsers (eg. 1 tab and a incognito tab, or chrome and firefox) you must have new session-variables. When you're open tabs in the same browser they share the same session.

How do you prevent a user from opening the same URL in multiple tabs in the same browser?

You cannot (and should not) do that. User can always just open use another browser or another computer to open another view onto the web site. So, basically you cannot ever prevent this. Your web-site should be able to handle multiple tabs viewing the same state.

How do you handle a session in multiple tabs in a browser in Java?

You can put in an hidden input in the page a value like the timestamp generated in the back-end when the page is requested. In this way every page will has a unique value. Then you can put a hashmap in session where the key - value pairs are the generated timestamp (key) and the object you need (value).


2 Answers

I figured out a solution :

  1. Using Javascript assign a unique id like a guid to the browser window / tab by assigning the guid value to the window.name property. window.name property is unique to each browser window/tab and won't be shared across the windows.

  2. Using the guid as the key, read and write data to your ASP.NET session via a webservice. Since javascript does not have access to asp.net session, you will need to use a webservice and call it's method through javascript.

The data can be transfered between javascript and webservice via JSON.

like image 120
ace Avatar answered Oct 04 '22 21:10

ace


  1. Give each page a guid. Store the guid in a hidden field.
  2. Use this guid as the session key that stores a struct with all the variables etc. for that page.
  3. You can now pull information for a specific page "rendering" and passes data to the session struct to the new "rendering" in the code-behind.

This is similar to what ViewState does automatically.

EDIT

Comment response

I don't think that's possible as you described it.

In your question you mentioned page 1 to 2 navigation. If the user types in the URL for page 2 in the browser then how do you tie that current page 2 rendering with the previous page 1?

You need something to do so, unless the code-behind can search a list of incomplete workflows that have stopped at page 1 and uses that as the previous guid value for the new page 2 rendering.

Comment response 2

You can find all the links on the page and modify those but it hints that something is wrong ( in my opinion ). But...

http://www.extremeexperts.com/Net/Articles/LoopingthroughControls.aspx

Gives you a simple way of processing all controls on the page. You can test to see if the control is a hyperlink easily. Not that all links will need that runat="server" parameter set for this to work.

like image 25
kervin Avatar answered Oct 04 '22 23:10

kervin