本文转自:http://jinaldesai.net/stop-sharing-session-state-between-multiple-tabs-of-browser/

Scenario: By default all browsers share session state between multiple tabs. So if you logged into one tab with particular site and open internal link of the same site in new tab, you need not to worry to login again. It will automatically declare you as logged in user.

For example, if I open google and login to google.com. Now I open new tab in the same browser and type gmail.com then it will automatically logged in me and redirect to google mail, here at this step it will not ask me to log in again.

Requirements: There are some situations where we need to stop sharing session between the browser tabs. 1. Requirement to force user to provide credentials for every new tab(or any other user identifying information). 2. If user retrieves particular record(lets say order information) into two different tabs. In one tab user deletes the record and in another tab user is trying to modify the record. In this situation data will be unstable and cause error. 3. If you are building secure site like e-banking or money transactional site then you need to force user to stay only on single tab, rather than using multiple tabs for doing money transactional activities. 4. If I am administrator of customer accounts in site. Now I am accessing account of customer “ABC” and then open new tab and access account of customer “PQR”. The session is now holding information for customer “PQR” and thinks that I am working on customer “PQR”. Now after working on customer “PQR”, I am coming back to customer “ABC” (tab) and start editing it. Actually according to session information it is assuming again that I am editing customer “PQR” and the problem starts here(data discrepancy).

Solutions: 1. Prevent user from opening new tab: I have found some javascript solutions which you can deploy at client side(obviously you need to worry about browser versions) to achieve this. 2. Identify page request per tab(uniquely) which help server to interprit the request as unique request for new tab even though it is for same record. In fact there are many ways we can achieve this in Asp.NET.

2.1 First Solution. The Simpler Solution: You can change config settings of Asp.NET site as following.

<configuration>
  <system.web>
      <sessionState mode="InProc" cookieless="UseUri"
          </sessionState>
  </system.web>
</configuration>

or

<configuration>
  <system.web>
    <sessionState cookieless="true"
      regenerateExpiredSessionId="true" />
  </system.web>
</configuration>

As you show, the value “UseUri” of cookieless attribute with sessionstate tag is set. This indicate to use cookieless session and in particular embed session identifying code into the URI. So each tab you open contains unique code for identifying the request as unique request in URI itself. The unique code is appended before the page name.

i.e. http://www.abc.com/SampleWebSite/(S(afdg3ires1ik0lmjm3pkjtzl))/Home.aspx

The only cons of this solution is that the URI is then distorted with some unique code. So user can not use it for bookmarking or promoting site. It is not generating bookmark friendly.

2.2 Second Solution. Manually generate unique page identification code and insert it into hidden field on every page.

<asp:HiddenField ID="PageID" runat="server" />

In the form load include following code. It will generate unique page identification code based on millisecond and other time component which always be unique for your site.

If Not IsPostaback Then
  'Generate a new PageiD'
  Dim R As New Random(DateTime.Now.Millisecond +
         DateTime.Now.Second * 1000 +
         DateTime.Now.Minute * 60000 +
         DateTime.Now.Minute * 3600000)
  PageID.Value = R.Next()
End If

So by this method you can identify each tab request as unique requests. But it has following two disadvantages.

First: The access of hidden element PageID is only when ViewState is restored on postback. So you cannot access PageID in page_init(). Second: As hidden field is accessible to the visitor, anyone can change PageID. So this solution will work only for environment with 100% trust ratio of all user.

2.3 Third Solution. 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. 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.

Conclusion: Even though it is feature of most browsers to share session between the tabs, there are some situations which requires to stop sharing session across multiple tab. Here in this article I have shared some tricks you can use to overcome this bottleneck. You can also share your solutions with me.

References: http://forums.asp.net/t/1098023.aspx/1 http://stackoverflow.com/questions/2840615/asp-net-session-multiple-browser-tabs-different-sessions http://msdn.microsoft.com/en-us/library/ms178581.aspx http://geekswithblogs.net/ranganh/archive/2009/04/17/asp.net-session-state-shared-between-ie-tabs-and-ie8.aspx http://www.codeproject.com/Answers/148538/Session-Sharing-in-multiple-tabs-in-IE-in-Asp-net#answer3 https://sites.google.com/site/sarittechworld/track-client-windows http://stackoverflow.com/questions/2829228/way-around-asp-net-session-being-shared-across-multiple-tab-windows

[转]Stop Sharing Session State between Multiple Tabs of Browser的更多相关文章

  1. ASP.NET MVC之Session State性能问题(七)

    前言 这一节翻译一篇有关Session State性能问题的文章,非一字一句翻译. 话题 不知道我们在真实环境中是否用到了Session State特性,它主要用来当在同一浏览器发出多个请求时来存储数 ...

  2. Unable to make the session state request to the session state server处理

    Server Error in '/' Application. Unable to make the session state request to the session state serve ...

  3. 在IIS上发布项目后浏览时报的错:Unable to make the session state request to the session state server

    错误描述: Unable to make the session state request to the session state server. Please ensure that the A ...

  4. Session State Pattern会话状态模式

    Client Session State 客户会话状态. 在Client端保存会话状态. 运行机制 Client在每次请求时会把所有的会话数据传给Server,Server在响应时把所有的会话状态传给 ...

  5. Unable to make the session state request to the session state server处理方法

    Server Error in '/' Application. Unable to make the session state request to the session state serve ...

  6. 关于ASP.NET Session State Server

    最近公司开发的一个网站因为访问量增大,需要添加多台Web Server来进行负载均衡. 但是在做负载均衡前需要将一些原来固定存储在单台机器上的东西分离出来,使之能单独存在在一个独立的机器上,其中就有S ...

  7. MVC之Session State性能

    ASP.NET MVC之Session State性能问题(七)   前言 这一节翻译一篇有关Session State性能问题的文章,非一字一句翻译. 话题 不知道我们在真实环境中是否用到了Sess ...

  8. 转: 解决【Unable to make the session state request to the session state server】

    错误描述: Unable to make the session state request to the session state server. Please ensure that the A ...

  9. what is diff. b/w app state & session state

    Application state is a data repository available to all classes in an ASP.NET application. Applicati ...

随机推荐

  1. 我所知道的Javascript

    javascript到了今天,已经不再是我10多年前所认识的小脚本了.最近我也开始用javascript编写复杂的应用,所以觉得有必要将自己的javascript知识梳理一下.同大家一起分享javas ...

  2. 【分享】纯js的n级联动列表框 —— 基于jQuery,支持下拉列表框和列表框,最重要的是n级,当然还有更重要的

    多个列表框联动,不算是啥大问题,但是却挺麻烦,那么怎么才能够尽量方便一点呢?网上搜了一下,没发现太好用的,于是就自己写了一个.基于jQuery,无限级联动,支持下拉列表框和列表框. 先说一下步骤和使用 ...

  3. ECharts – 大数据时代,重新定义数据图表

    ECharts 基于 Canvas 的纯 Javascript 图表库,提供直观,生动,可交互,可个性化定制的数据可视化图表.创新的拖拽重计算.数据视图.值域漫游等特性大大增强了用户体验,赋予了用户对 ...

  4. Dynamics.js - 创建逼真的物理动画的 JS 库

    Dynamics.js 是一个用来创建物理动画 JavaScript 库.你只需要把dynamics.js引入你的页面,然后就可以激活任何 DOM 元素的 CSS 属性动画,也可以用户 SVG 属性. ...

  5. Winform 图片鼠标滚动查看(放大,缩小,旋转,拖动查看)[日常随笔]

    方法千千万,我只是其中一笔[通过控制PictureBox来控制图片,图片完全施展在控件中]...几久不做,还真有点陌生! 窗体构造中添加鼠标滚动: /// <summary> /// 窗体 ...

  6. JavaScript 随机链接

    <html> <body> <script type="text/javascript"> var r=Math.random() if (r& ...

  7. EXCEL快速自动填充方法集锦

    EXCEL快速自动填充方法集锦 原文地址,转载请注明:http://www.cnblogs.com/croso/p/5396841.html 方法一: 名称框输入a1:a1000回车,1, ctrl+ ...

  8. Win7重装系统遇到的问题以及MysQL的问题解决

    连续三天因为系统的错误,android方面的软件一直不能正确运行.而且每次开机的时候QQ 微信等聊天工具也出现损坏.虽然重新下载一个可以保证在电脑不管的情况下正常的运行.可是作为玩电脑时间不长的我来说 ...

  9. Android 中的编码与解码

    前言:今天遇到一个问题,一个用户在登录的时候,出现登录失败.但是其他用户登录都是正常的,经过调试发现登录失败的用户的密码中有两个特殊字符: * .#  . 特殊符号在提交表单的时候,出现了编码不一样的 ...

  10. xcode7无证书真机调试 Error: An App ID with identifier "*" is not avaliable. Please enter a different string.

    1. Error: An App ID with identifier "*" is not avaliable. Please enter a different string. ...