Is it possible to not share Session variables between different browser tabs?
I want to keep one login session between tabs, but don't want to share information between opened tabs.
is it possible?
Is it possible to not share Session variables between different browser tabs?
I want to keep one login session between tabs, but don't want to share information between opened tabs.
is it possible?
No. That is not possible with the session cookies, which are controlled by your browser. You have no control whatsoever if the cookies are reused in other tabs (usually they will, but it is possible browser vendors will deviate from that).
You might be looking for 'view state', hidden input fields or similar approaches to let the state travel with the page, instead of the session.
You can implement your own PageSession which basically adds a prefix to your session variables. It could sound quite big to implement at first, but it's not a big deal.
Create a BasePage
First, let's create a BasePage class. This class will be a normal class that inherits from Page.
We will create there a property called PageInstanceUID
public string PageInstanceUID
{
get
{
ViewState["PageInstanceUID"];
}
set
{
ViewState["PageInstanceUID"] = value;
}
}
Then we assign a unique value to that property on load:
if(!IsPostBack)
{
PageInstanceUID = new FileInfo(Request.PhysicalPath).Name + Guid.NewGuid().ToString();
}
Create a PageSession class
The PageSession class is also pretty simple:
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;
}
}
Create a PageSession in your BasePage class
Now you need to create a PageSession class in your BasePage.
public PageSession PageSession
{
get
{
return _pageSesion;
}
}
Connect the dots
And last, edit your pages to inherit from BasePage instead of Page.
In ASP.NET Core, Session variables are common to all tabs, but you can update them each time you switch between browser tab by using ajax posts with jquery:
function sendFiltersToServer() {
$.ajax({
type: 'POST',
url: '@Url.Action("UpdateSessionVariables")',
data: {
"selectedDepartmentId": $('#ddlDepartments').val() ?? 0,
"selectedEmployeeId": $('#ddlEmployees').val() ?? 0
}
});
}
$(function () {
//document.onmouseenter = sendFiltersToServer;
window.onfocus = sendFiltersToServer;
});
Above script must then be placed within each page containing the variables to be updated. On the server side, in your ControllerBase class (from which all your other controllers derive), you place the called function:
public void UpdateSessionVariables(int selectedDepartmentId, int selectedEmployeeId)
{
SelectedDepartmentId = selectedDepartmentId;
SelectedEmployeeId = selectedEmployeeId;
}
protected int SelectedDepartmentId
{
get
{
int? selectedDepartmentId = HttpContext.Session.GetInt32("SelectedDepartmentId");
return selectedDepartmentId ?? DEFAULT_DEPARTMENT_ID;
}
set
{
HttpContext.Session.SetInt32("SelectedDepartmentId", value);
}
}
Above solution works as long as the user keeps one of the tabs utilized at any point in time. If your application needs several tabs processing the variables concurrently (for exemple in server sent events or websockets), then you need a unique tabId generated like so:
$(function () {
var tabID = sessionStorage.tabID && sessionStorage.closedLastTab !== '2'
? sessionStorage.tabID
: sessionStorage.tabID = Math.random();
sessionStorage.closedLastTab = '2';
$(window).on('unload beforeunload', function () {
sessionStorage.closedLastTab = '1';
});
});
On each ajax call, you pass the tabId which you concatenate to your session variable names.