会话在同一浏览器上的不同选项卡中重叠

问题描述:

如果用户在同一个浏览器上打开网站,但在不同的标签中会话变量重叠,则会话重叠。

由于某些原因,我不想使用cookieless = true安全理由。请给我一些替代方法来摆脱这个问题。

分享一些示例代码以供参考或链接。



先谢谢

The sessions are overlapped if the user open the website on a same browser but in different tabs then the session variables were overlapped.
I do not want to use "cookieless=true" due to some security reason. Please give me some alternate method to get rid of this problem.
Share some sample code for reference or link.

Thanks in Advance

也许你可以在url参数中使用一个标记。然后根据该令牌取消所有内容。在新选项卡上,用户将不会拥有令牌...除非当然,用户将URL从一个选项卡复制并粘贴到另一个选项卡...在这种情况下,您知道使用相同的会话密钥。 />
如果此令牌字段不存在,那么您就知道要开始一个新会话,然后为新用户分配一个令牌。



Perhaps you could use a token in the url parameter. Then base everything off that token. On a new tab, the user won't have the token... unless of course, the user copied and pasted the url from one tab to the other... In which case you know to use the same session keys.
If this token field is not present, then you know to start a new session and then assign the new user a token.

bool hasToken = true;
string token = Request["token"];
string myTempString = "Important Value here";
int userId = 42;
if(string.IsNullOrEmpty(token)){
    token = Guid.NewGuid().ToString();
    hasToken = false;
}


if(hasToken){
    userId = (int)Session["user_" + token];
    myTempString = Session["otherkey_" + token].ToString(); 
}
else{
    Session["user_" + token] = userId; // Idk, assign something here...
    Session["otherkey_" + token] = myTempString;
    // Keep it formatted like that.. or some other format 
    // that includes the token in the session key
}





这样,你可以在同一个浏览器中保持多个会话。



This way, you can keep multiple sessions going from the same browser.