简体   繁体   English

如何在.Net C#中处理并发会话

[英]How to Handle Concurrent sessions in .Net C#

I am working on an application where we are saving some member's (not logged in user) information in session. 我正在开发一个应用程序,该应用程序在会话中保存一些成员(未登录的用户)信息。

That specific ID is used to take payments and do other things as well. 该特定ID用于付款和执行其他操作。 However, when i open the two tabs of that application or two windows of that application; 但是,当我打开该应用程序的两个选项卡或该应用程序的两个窗口时; the member's session id mismatches. 成员的会话ID不匹配。 The current tab picks up the ID of the secondary tab which is opened. 当前选项卡获取已打开的辅助选项卡的ID。

I request for some guidance/help. 我要求一些指导/帮助。

I am using following code: 我正在使用以下代码:

public static object GetSessionValue(string sessionName) {
    if (HttpContext.Current.Session[sessionName] == null) {
        return null;
    } else {
        return HttpContext.Current.Session[sessionName];
    }
}

public static void SetSessionValue(string sessionName, object sessionValue) {
    HttpContext.Current.Session[sessionName] = sessionValue;
}

However, when i open the two tabs of that application or two windows of that application; 但是,当我打开该应用程序的两个选项卡或该应用程序的两个窗口时; the member's session id mismatches. 成员的会话ID不匹配。 The current tab picks up the ID of the secondary tab which is opened. 当前选项卡获取已打开的辅助选项卡的ID。

If you open the same website in two different tab of a browser, they share the session. 如果您在浏览器的两个不同选项卡中打开同一网站,则它们将共享会话。 It is by design. 这是设计使然。

You can replicate it. 您可以复制它。 Login to bankofamerican.com, and open a new tab and paste https://secure.bankofamerica.com/transfers/funds-transfer.go Notice that you do not need to login. 登录到bankofamerican.com,然后打开一个新标签并粘贴https://secure.bankofamerica.com/transfers/funds-transfer.go 注意,您无需登录。

However, If you paste the url in new browser window (while logging in one browser window - Must be same type of browser ), they something share the session. 但是,如果将URL粘贴到新的浏览器窗口中(在一个浏览器窗口中登录- 必须是相同类型的浏览器 ),则它们共享会话。

The Session object is used to share state for a series of page loads on a given browser. Session对象用于共享给定浏览器上一系列页面加载的状态。 This is great for storing information like what user is logged in, and what permissions the user has because it is the same human (AKA user) behind the browser for each page load. 这对于存储诸如用户登录名以及用户具有的权限之类的信息非常有用,因为每次加载页面时,它都是浏览器背后的同一个人(AKA用户)。

The problem you describe is that you want two tabs of the same browser to show different states, aka if tab 1 is on the homepage, and tab2 is on the account page, you do not want tab 2 refresh to load homepage, you want it to stay on the account page. 您描述的问题是,您希望同一浏览器的两个选项卡显示不同的状态,也就是说,如果选项卡1位于主页上,而选项卡2位于帐户页面上,则您不希望选项卡2刷新以加载主页,留在帐户页面上。 This is a page-level state, not a user-level state, and so you should not be storing your information in the Session. 这是页面级别的状态,而不是用户级别的状态,因此您不应将信息存储在Session中。

If you are using WebForms (.aspx pages) to run your site, you should take a look at ViewState on MSDN . 如果使用WebForms(.aspx页)运行站点,则应查看MSDN上的ViewState If you are using MVC, you should consider using javascript and AJAX to update the page and store the state of the pages in js variables client-side. 如果您使用的是MVC,则应考虑使用javascript和AJAX更新页面并将页面状态存储在客户端的js变量中。

All of the tabs opened in the same browser share the same ASP session on the server side. 在同一浏览器中打开的所有选项卡在服务器端共享相同的ASP会话。 So the general solution is to embed enough information in the web page (browser tab) itself so that a postback from it can be used to retrieve the appropriate conversation from within the shared session. 因此,一般的解决方案是在网页本身(浏览器选项卡)中嵌入足够的信息,以便可以使用其回发来从共享会话中检索适当的对话。

In other words, write some key value in hidden fields(s) within the page, which will be sent back as part of the postback request, to distinguish each browser tab from the others. 换句话说,在页面的隐藏字段中写入一些键值,这些键值将作为回发请求的一部分发送回去,以区分每个浏览器选项卡。 A lot of this can be handled with judicious use of the ASP viewstate of the page. 明智地使用页面的ASP viewstate可以处理很多此类情况。 I believe you can also force the viewstate values to be be encrypted. 我相信您也可以强制将viewstate值加密。

Alternatively, you can encode a hidden field yourself with with a random key that can be used to retrieve data you've saved in the session cache. 另外,您可以使用随机密钥自己对隐藏字段进行编码,该随机密钥可用于检索保存在会话缓存中的数据。 This approach is harder to do right, though, since you have do deal with removing old (expired) keys from the session if the user closes his tabs or browser. 但是,这种方法很难正确执行,因为如果用户关闭了标签页或浏览器,您确实要从会话中删除旧的(过期的)键。

For example, I have a web application that shows data lists in fixed-length pages. 例如,我有一个Web应用程序,它以固定长度的页面显示数据列表。 Since I want the user to be able to open multiple lists simultaneously, each list in a separate browser tab, I embed the current paging info (page number, list number, etc.) in hidden fields in the HTML page. 因为我希望用户能够同时打开多个列表,所以每个列表都在单独的浏览器选项卡中,因此我将当前页面信息(页面编号,列表编号等)嵌入到HTML页面的隐藏字段中。 I then read these hidden field values on postback to resume the proper paging context for that particular browser tab that performed the postback. 然后,我在回发时读取这些隐藏字段的值,以恢复执行该回发的特定浏览器选项卡的正确页面上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM