vb.net 为每个浏览器窗口或选项卡创建唯一的会话变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13685806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create Unique Session Variable per Browser Window or Tab
提问by GoldBishop
Having a slight problem with creating a .Net Session based variable.
创建基于 .Net Session 的变量时有一个小问题。
I know i can just use the Sessionextension and be done with it but i am looking more for a Multiple Browser (either Window or Tab, for IE) situation on a single desktop.
我知道我可以只使用Session扩展程序并完成它,但我更多的是在单个桌面上寻找多浏览器(窗口或选项卡,对于 IE)的情况。
Currently, i have a Module declared in the Web-App and when i open up two individual Windows of IE they initially load the custom PageRoutingdesign, as expected but once i go to the next step with both windows open, the last one to be opened is the design/logic used for the rest of the application.
目前,我在 Web 应用程序中声明了一个模块,当我打开两个单独的 IE 窗口时,它们最初PageRouting按预期加载自定义设计,但是一旦我在两个窗口都打开的情况下进入下一步,最后一个要打开是用于应用程序其余部分的设计/逻辑。
Module.vb
模块.vb
Namespace classes
Module Globals
Public Brand As Brand
Public Test As Test
Public Results As Results
Public Primary As String = "#FFFFFF"
Public Secondary As String = "#FFFFFF"
Public Terteruary As String = "#FFFFFF"
End Module
End Namespace
In code, i reference the objects as Globals.Brandor Globals.Primarybut in either case a situation where the same desktop could open up the same website with different PageRoutingaddress, it assumes the last opened browser window.
在代码中,我将对象引用为Globals.Brand或Globals.Primary但在任何一种情况下,同一个桌面可以用不同的PageRouting地址打开同一个网站,它假定最后打开的浏览器窗口。
The Brand& Testvariables are initialized in the Session_Startevent in Globals.asax. All references to these objects are explicit references using Globals.<variable>annotation when used. Resultsis initialized on first use during the execution of the website.
在Brand与Test变量在初始化Session_Start事件Globals.asax。对这些对象的所有引用都是使用Globals.<variable>时使用注解的显式引用。 Results在网站执行期间首次使用时进行初始化。
Question
题
- How do i make sure that each individual browser window is loaded with its own unique session cache for use with the site?
- 我如何确保每个单独的浏览器窗口都加载了自己唯一的会话缓存以供站点使用?
Updated - 2012-12-03
更新 - 2012-12-03
What about a design like this?
这样的设计怎么样?
Public Class Class1
private _sess as HTTPSessionState
...
private readonly property Session as HttpSessionState
Get
if _sess is nothing then
_sess = httpcontext.current.session
end if
return _sess
End Get
end property
...
public property Primary as string
Get
return cstr(session("primary"))
end get
Set(value as string)
session("primary") = value
end set
end property
...
end class
With Class1 being instantiated at the Master/Content page level?
在主/内容页面级别实例化 Class1 吗?
Update #2 - 2012-12-03
更新 #2 - 2012-12-03
Modified the module, let me know if this is a viable session control setup
修改了模块,让我知道这是否是一个可行的会话控制设置
Module Globals
'Dictionary Collection of type (SessionID:String, Quiz:classes.Quiz)
Public Quizzes As Dictionary(Of String, classes.Quiz)
Public Property Quiz(session As String) As Quiz
Get
Return Quizzes(session)
End Get
Set(value As Quiz)
Quizzes(session) = value
End Set
End Property
End Module
Final Form 2012-12-10:
最终表格 2012-12-10:
Module.vb
模块.vb
Module Globals
'Get/Set a Quiz object into the SessionState.
Public Property Quiz(sess As HttpSessionState) As Quiz
Get
Return CType(sess("quiz"), Quiz)
End Get
Set(value As Quiz)
sess("quiz") = value
End Set
End Property
End Module
Web.config
网页配置
<system.web>
...
<sessionState mode="InProc" cookieless="UseCookies"
regenerateExpiredSessionId="true" timeout="20"
cookieName="ASPNET_Quiz" />
...
</system.web>
The above form worked as expected utilizing the indexer aspect of a Property. Havent had any user instance problems. One side note, is that in order for this to work effectively the user must close all browser windows and open a new window for the session to clear out
上面的表单使用属性的索引器方面按预期工作。没有遇到任何用户实例问题。一方面要注意的是,为了使其有效工作,用户必须关闭所有浏览器窗口并打开一个新窗口以清除会话
回答by Carlos Landeras
The problem you are coming across is a very common one in web programming. A Module's members are static- meaning there is one instance of them across the entire AppDomain of your application. Every user that accesses these will get the same object.
您遇到的问题是 Web 编程中非常常见的问题。模块的成员是静态的——这意味着它们在应用程序的整个 AppDomain 中只有一个实例。访问这些的每个用户都将获得相同的对象。
You could possibly replace the public variable in your module with a property whose getter you write to access a user-specific field in a dictionary (please remember thread safety when writing this getter code).
您可以将模块中的公共变量替换为您编写其 getter 以访问字典中特定于用户的字段的属性(请记住编写此 getter 代码时的线程安全性)。
The much easier solution would be to use the Session. Session values are stored server-side and are user specific. The only thing that get's sent client side is the session key, and if you are using .Net authentication, this is likely already getting sent.
更简单的解决方案是使用 Session。会话值存储在服务器端并且是特定于用户的。get 发送给客户端的唯一内容是会话密钥,如果您使用 .Net 身份验证,这很可能已经发送了。
Check this source:
检查这个来源:
How to get a public variable (in a Module) to NOT share value between users

