In ASP.NET (server side), how can I uniquely identify one browser window from another that are under the same cookiedbased sessionId

Users of my web application can open more than one browser window (or tab) and specify the same page. We use cookie-based session identifiers, and the user usually works with the same session identifier in both browsers / tabs. I would like to be able to uniquely identify which browser window (and tab) the ASP.NET page requested (to make sure that the data stored in the session is not mixed).

(for example, I would be glad if the browser generated and sent the window / tab id with the http request, since it publishes HTTP_USER_AGENT)

Any ideas?

- thomas

+6
browser session
source share
4 answers

If I were going to implement something like this, I would probably start with Dictionary<Type, List<Guid>> and save it in a user session. I will also probably make it a special type that delegates a dictionary and has a factory method that works similarly

 public Guid GeneratePageIdentifier(Page thepage) { var guid = Guid.New(); if(_dictionary[thepage.GetType()] == null) _dictionary[thepage.GetType()] = new List<Guid> { guid }; else ((List<Guid>)_dictionary[thepage.GetType()]).Add(guid); return guid; } 

Then insert the guide that returned from this method on the VIewState page. In your methods of the page that perform the actions that you need to check which page, you could check that the guid is inside the collection, to do something. You might also want to implement a custom type with the guid property to request more information about why you are doing this or what you need for its significance.

+2
source share

The viewstate on each page will be different, maybe you can use some unique identifier created on each loaded page?

+2
source share

By default, this is not possible due to the statelessness of the website, but you can add a “page identifier”, which is generated with each open page and transmitted for each action.

I would recommend that you reorganize the application so that these mixes cannot occur, regardless of which page / tab / window the request comes from.

+1
source share

As Marc Redman said, you can use Viewstate + Session to store values ​​specific to this page. ViewState is good for storing a key (string), a session for storing any type of complex objects.

Use ViewState or a hidden field to load the first time the GUID is called.

  public string PageUid { get { if (ViewState["UID"] == null) ViewState.Add("UID", Guid.NewGuid().ToString()); return ViewState["UID"].ToString(); } } 

Then use the session to get / set your values ​​with this key:

  string MyPagesessionVariable { get { if (Session["MYVAR" + PageUid] == null) { Session["MYVAR" + PageUid] = "VALUE NOT SHARED WITH OTHER TABS/WINDOWS"; } return Session["MYVAR" + PageUid]; } set { Session["MYVAR" + PageUid] = value; } } 
+1
source share

All Articles