I'd like to maintain a session state per browser tab.
我想维护每个浏览器标签的会话状态。
Is this easy (or even possible) to do in ASP.NET?
这在ASP.NET中很容易(甚至可能)实现吗?
Example: A user hits Ctrl-T in firefox 5 times and visits the site in each tab. I'd like each tab to have its own session state on the server
示例:用户在firefox中按下Ctrl-T 5次,并在每个选项卡中访问站点。我希望每个选项卡在服务器上有自己的会话状态。
3 个解决方案
#1
21
To facilitate multi-tab session states for one user without cluttering up the URL, do the following.
要为一个用户提供多选项卡会话状态,而又不影响URL,请执行以下操作。
In your form load function, include:
在表单加载函数中,包括:
If Not IsPostback Then
'Generate a new PageiD'
ViewState("_PageID") = (New Random()).Next().ToString()
End If
When you save something to your Session State, include the PageID:
当你保存一些东西到你的会话状态时,包括PageID:
Session(ViewState("_PageID").ToString() & "CheckBoxes") = D
Notes:
注:
- As with session ID's in general, you cannot trust that malicious viewers will not change the SessionID / PageID. This is only a valid solution for an environment where all users can be trusted. Fortunately, ViewState does offer more protection than using a hidden input field.
- 与会话ID一般,您不能相信恶意的查看器不会改变SessionID / PageID。对于所有用户都可以信任的环境,这只是一个有效的解决方案。幸运的是,ViewState比使用隐藏的输入字段提供更多的保护。
- You will not have access to the PageID until the ViewState is restored upon PostBack. Therefore, you will not have access to the PageID in the page_init() handler.
- 在回发恢复ViewState之前,您不能访问PageID。因此,您将无法访问page_init()处理程序中的PageID。
#2
15
<configuration>
<system.web>
<sessionState cookieless="true"
regenerateExpiredSessionId="true" />
</system.web>
</configuration>
http://msdn.microsoft.com/en-us/library/ms178581.aspx
http://msdn.microsoft.com/en-us/library/ms178581.aspx
in this case each tab will get unique ID and it will looks like it is another visitor.
在这种情况下,每个选项卡都有唯一的ID,看起来就像另一个访问者。
#3
1
Using Brian Webster's answer I found a problem with XMLHttpRequests. It turned out, XMLHttpRequests did not set the IsPostback
flag to true and therefore the request looked like a new request and one would end up having a new session state for that request. To solve that problem I also checked the value of the ViewState("_PageID")
使用Brian Webster的答案,我发现了一个xmlhttprequest问题。事实证明,xmlhttprequest并没有将IsPostback标记设置为true,因此这个请求看起来就像一个新的请求,一个请求最后会有一个新的会话状态。为了解决这个问题,我还检查了ViewState的值(“_PageID”)
so that my code looks like this in C#:
我的代码在c#中是这样的
protected dynamic sessionVar; //a copy of the session variable
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && ViewState["_PageID"] == null)
{
ViewState["_PageID"] = (new Random()).Next().ToString();
Session[ViewState["_PageID"] + "sessionVar"] = initSessionVar(); //this function should initialize the session variable
}
sessionVar = Session[ViewState["_PageID"] + "sessionVar"];
//...
}
#1
21
To facilitate multi-tab session states for one user without cluttering up the URL, do the following.
要为一个用户提供多选项卡会话状态,而又不影响URL,请执行以下操作。
In your form load function, include:
在表单加载函数中,包括:
If Not IsPostback Then
'Generate a new PageiD'
ViewState("_PageID") = (New Random()).Next().ToString()
End If
When you save something to your Session State, include the PageID:
当你保存一些东西到你的会话状态时,包括PageID:
Session(ViewState("_PageID").ToString() & "CheckBoxes") = D
Notes:
注:
- As with session ID's in general, you cannot trust that malicious viewers will not change the SessionID / PageID. This is only a valid solution for an environment where all users can be trusted. Fortunately, ViewState does offer more protection than using a hidden input field.
- 与会话ID一般,您不能相信恶意的查看器不会改变SessionID / PageID。对于所有用户都可以信任的环境,这只是一个有效的解决方案。幸运的是,ViewState比使用隐藏的输入字段提供更多的保护。
- You will not have access to the PageID until the ViewState is restored upon PostBack. Therefore, you will not have access to the PageID in the page_init() handler.
- 在回发恢复ViewState之前,您不能访问PageID。因此,您将无法访问page_init()处理程序中的PageID。
#2
15
<configuration>
<system.web>
<sessionState cookieless="true"
regenerateExpiredSessionId="true" />
</system.web>
</configuration>
http://msdn.microsoft.com/en-us/library/ms178581.aspx
http://msdn.microsoft.com/en-us/library/ms178581.aspx
in this case each tab will get unique ID and it will looks like it is another visitor.
在这种情况下,每个选项卡都有唯一的ID,看起来就像另一个访问者。
#3
1
Using Brian Webster's answer I found a problem with XMLHttpRequests. It turned out, XMLHttpRequests did not set the IsPostback
flag to true and therefore the request looked like a new request and one would end up having a new session state for that request. To solve that problem I also checked the value of the ViewState("_PageID")
使用Brian Webster的答案,我发现了一个xmlhttprequest问题。事实证明,xmlhttprequest并没有将IsPostback标记设置为true,因此这个请求看起来就像一个新的请求,一个请求最后会有一个新的会话状态。为了解决这个问题,我还检查了ViewState的值(“_PageID”)
so that my code looks like this in C#:
我的代码在c#中是这样的
protected dynamic sessionVar; //a copy of the session variable
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && ViewState["_PageID"] == null)
{
ViewState["_PageID"] = (new Random()).Next().ToString();
Session[ViewState["_PageID"] + "sessionVar"] = initSessionVar(); //this function should initialize the session variable
}
sessionVar = Session[ViewState["_PageID"] + "sessionVar"];
//...
}