Different ASP.NET sessions for each browser tab

Is it possible to share session variables between different browser tabs?

I want to save one login session between tabs, but I do not want to share information between open tabs.

Is it possible?

+4
source share
2 answers

No. This is not possible with session cookies that are controlled by your browser. You have no control if cookies will be reused on other tabs (as a rule, they will be, but it is possible, it will deviate from the suppliers).

You can search for “view state”, hidden input fields, or similar approaches to allow the state to navigate with the page rather than the session.

+2
source

PageSession, . , .

BasePage. , Page.

, PageInstanceUID

public string PageInstanceUID 
{
    get
    {
        ViewState["PageInstanceUID"];
    }
    set
    {
        ViewState["PageInstanceUID"] = value;
    } 
}

:

if(!IsPostBack)
{
    PageInstanceUID = new FileInfo(Request.PhysicalPath).Name + Guid.NewGuid().ToString();
}

PageSession

PageSession :

public class PageSession
{
  readonly BasePage _parent;

  public PageSession(BasePage parent)
  {
    _parent = parent;
  }


  public object this[string name]
  {
    get
    {
      return _parent.Session[GetFullKey(name)];  
    }
    set
    {
      _parent.Session[GetFullKey(name)] = value;

    }
  }

  public string GetFullKey(string name)
  {
    return _parent.PageInstanceUID + name;
  }
}

Session BasePage

PageSession BasePage.

 public PageSession PageSession
 {
   get
   {
     return _pageSesion;
   }
 }

, , , BasePage Page.

+1

Source: https://habr.com/ru/post/1692651/


All Articles