开发者

ASP.NET: Session Variable Question

As a quazi-newbie to asp.net, I have a question about session variables.

I'm building an intranet application for my company. Naturally it is a beast, and there are some variables (class objects) that need to be stored. Say, a class object named 'Driver' that has FirstName [String] and LastName [String].

Now say I store the object like this:

Dim objDriver as Driver

objDriver.FirstName = "Joe"
objDriver.LastName = "Smith"

Session.Contents("Driver") = obj开发者_StackOverflow中文版Driver

This code seems to execute and function quite well. Now my question is, if Suzy logs on a different computer and tries the application, will she see Joe's information when she executes

Dim objDriver as Driver

objDriver = Session.Contents("Driver")

If anyone could shed any light on this it would be greatly appreciated.

Thanks,

Jason


No, Suzy will not see anything. To make that possible use the Application cache...

Sessions are identified by a unique identifier that can be read by using the SessionID property. When session state is enabled for an ASP.NET application, each request for a page in the application is examined for a SessionID value sent from the browser. If no SessionID value is supplied, ASP.NET starts a new session and the SessionID value for that session is sent to the browser with the response.

http://msdn.microsoft.com/en-us/library/ms178581.aspx

Instead use:

dim foo = HttpContext.Current.Application("foo");

and

HttpContext.Current.Application("foo") = foo;


Session state is specific to the browser session. So no, Susie won't see Joe's data. Even if Joe opens a new browser window on the same computer, he won't be in the same session anymore.

Application state may be a different story -- and you can look into that if you actually want to share variables in such a wide scope.


Session is created per session as it can be understood. If you log in using, say, google Chrome and creates some variables, you'll not be able to access those variables from Firefox from the same computer.


Session objects exist per user per browser. So Suzy would have a session on Firefox but a different one on Internet Explorer. This is because each browser will have a unique cookie assigned.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜