Method
Session
Description
Session refers to two things in your web projects. The Session class in the project is a subclass of WebSession that you can use to add your own properties, methods and constants. Each user that connects to the web app gets their own instance of the Session class, which can be conveniently accessed using the Session method. To learn about the Session class, refer to the WebSession class. This topic discusses the Session method that provides you with a reference to WebSession for the active user.
Usage
Session.Property = Value
or
Session.Method
Part |
Description |
---|---|
Property |
Any valid property of the WebSession class or the Session subclass in the project. |
Value |
A value of the proper datatype for the Property. |
Method |
Any valid method of the WebSession class or the Session subclass in the project. |
Notes
Note
To refer to the current Session from within the Session object, use Self instead of Session as the prefix, especially in the Opening event because it may return Nil.
In order to see WebSession properties in the debugger, you can create a temporary variable:
#If DebugBuild Then
Var currentSession As Session = Session
#Endif
The reason for the pragma is that the call can be very expensive when there are lots of sessions running. This way it'll only affect performance when you're debugging.
Session context
Session may return Nil if the code is running on the main thread. This includes code that is run in the App object and code that is not part of the user interface, such as code in a Thread.
As long as a Session is not Nil, it is available for use:
If Not (Session = Nil) Then
Var user As String
user = Session.UserName ' your own property added to Session
End If
Multiple browser windows
If the user opens two browser windows/tabs to an app, the new window gets its own Session instance. Use cookies to determine if two sessions belong to the same user.
Sample code
This code saves the User Name and Password from a login page to properties added to the Session class:
Session.UserName = UserNameField.Text
Session.Password = PasswordField.Text
This code gets the current Session associated with a web page:
Var sc As WebSessionContext
sc = New WebSessionContext(App.SessionForControl(Self))
Compatibility
All project types on all supported operating systems.
See also
WebSession, WebSessionContext, WebApplication classes