Context.Session Object


Context.Session object properties and methods.

The Context.Session object contains properties and methods available for working with a server session. On the stand-alone application server session data is stored in memory in the same process as the application server. On IIS session data can be stored in one of many different ways. Session data can be stored in process like the stand-alone application server, in a state server which is another process on the same machine as the web server, or in a database (SQL or NoSql) depending on the session state provider that is selected for the application. Methods on the session object may have different performance characteristics depending on the session storage that is used.

The Context.Session object has a CallResult property. Use this property to see if a method call or property get succeeds before using a method's returned value or property's value. See the following examples:
dim errorMsg as C = ""
Context.Session.Add("lastVisitedPage", "secret")
if .not. Context.Session.CallResult.Success then
     'Note: This error is most likely caught and handled and does not cause a page error. 
end if



Context.Session.CallResult is used to check the success or failure of a method call or property get or set in the Context.Session object. See the note in the Context.Session class description above for more details and example. Read Only


Context.Session.CookieMode indicates whether the application is configured for cookieless sessions.


Context.Session.CookieName is the name of the cookie used to store the sessions id.

Count Numeric

Context.Session.Count is the number of items in the session-state collection. Read Only.


Context.Session.IsCookieless indicates where the session id is returned to the client. If .T. the session id is added to the URL. Otherwise, the session id is stored in a cookie. Read Only.


Context.Session.IsNewSession if .T. indicates the session was created during the current request. Otherwise, the session was created by some prior request. Read Only.


Context.Session.IsReadOnly if .T. indicates the session is read-only. Otherwise, the session is read-write.


Context.Session.IsSynchronized if .T. indicates that access to the session-state variables is thread safe. Otherwise, manual synchronization of session-state variable access needs to be done. Read Only


Context.Session.Keys is a collection of all the keys in the session-state. Read Only.


Context.Session.Mode specifies how the session-state is stored. Read Only.


Context.Session.SessionId is the unique identifier for the session. Read Only


Context.Session.Timeout gets or sets the time, in minutes, for a session to be terminated due to inactivity. Read Write.


Context.Session.Timestamp is the timestamp for when the session was created. The value is local time on the server. Read Only.


Context.Session.UniqueDataKey generated a unique value each time it is accessed. The value is the 32 digits of a Guid prepended with "DTA" resulting in a value that looks like this: DTA114ea94b16924e30ace6bef15c75d8f4. Read Only.


Context.Session.Variables is the way to access session-state collection values directly. Using Context.Session.Variables.MySessionVar = "MySessionVarValue" is equivalent to using Context.Session.Add("MySessionVar", "MySessionVarValue"). Note that Context.Session.MySessionVar is also equivalent to Context.Session.Variables.MySessionVar, but that Context.Session.Variables.MySessionVar is preferred. See an example .a5w page below.

The Server Programming Changes for Version 11 states that session variables can only be strings and that other variables types are not supported and may not work as expected. This also means that dots (".") are not allowed in the session variable name. That is, Context.Session.Variables.MySessionVar.FirstName is not allowed.
<head> </head> 
  Context.Session.Variables.ASessionVar = "Value for ASessionVar." 
  Context.Session.Add("AnotherSessionVar", "Value for AnotherSessionVar.")
  ? *html_escape(property_to_string(Context.Session.Variables)) + crlf() 
  Context.Session.SaveData("ASessionVar", "Changed value for ASessionVar.") 
  ?*html_escape(property_to_string(Context.Session.Variables)) + crlf() 


Abandon Method

terminates the current session.

Add Method

Adds a named value to the session-state collection.

Clear Method

Empties the entire session-state collection.

DeleteSessionFile Method

Deletes the value session-state key from the session-state collection under IIS.

FormatFileDataURL Method

Returns a URL suitable for a browser to retrieve a session file.

GetData Method

Gets the value of a specific key from the session-state collection.

GetDataFromFile Method

Gets the contents of a file previously stored in the current session and identified by the key value.

GetSQLCredentials Method

Returns the SQL username and password used for SQL connections.

HasVariable Method

Checks whether the session-state has a variable or not.

Remove Method

Removes a named value to the session-state collection.

RemoveAll Method

Alias of Context.Session.Clear().

RemoveAt Method

Removes the item at the specified 0-based index from the session-state collection.

SaveData Method

Sets the value of a key in the session-state collection.

SaveDataAsFile Method

Saves the value to the current session as a file identified by FileName.

SaveFileToSessionFile Method

Saves source file content to the current session using the provided key.

SaveSessionFileToFile Method

Retrieves contents previously stored in the current session as a file and copies them to a local file.

SetSQLCredentials Method

Sets the SQL username and password used for SQL connections.