Get/Kill user sessions

You can use the following functions or APIs to manage user session(s) for the installable cloud application:

  • The GetSessionID function of the PowerBuilder Application object -- Gets the session ID of the current client.

  • The LoadAll API provided by the SessionController.cs file in the PowerServer C# solution -- Gets the session IDs of all clients. Refer to View the API documentation for how to view the documentation of this API.

  • The KillByID API provided by the SessionController.cs file in the PowerServer C# solution -- Kills the session(s) according to the ID. Refer to View the API documentation for how to view the documentation of this API.

To get the session ID of the current application client, you can write PowerScripts as below:

String ls_SessionID
ls_SessionID = Getapplication().GetSessionID()

To get all user sessions, you can write PowerScripts as below:

//-------------------------------------LoadAll-----------------------------------------

httpclient  lhc_client
string ls_url
string ls_json

lhc_client = create httpclient

//GetSessions
ls_url = "http://localhost:5000/api/Session/LoadAll"
//This URL should be replaced with the actual IP address and port number of PowerServer Web APIs
//If there are multiple PowerServer Web API servers, obtain one by one
//lhc_client.SetRequestHeader("Authorization", $token, true)  //If authentication is enabled
lhc_client.sendrequest("Get",ls_url)

if lhc_client.getresponsestatuscode() = 200 then
                lhc_client.getresponsebody(ls_json)
                //parse the json
                //wf_getsessions(ls_json)
end if

//-------------------------------------------------------------------------------------------------

To kill the specified user session, you can write PowerScripts as below:

Step 1: Get all the user sessions first.

Step 2: Kill the specified session according to the session ID.

The session information returned will look like this:

//[{"sessionid":"8e3f5c6d-7515-4377-9a45-0e3349fcbfd2","application":"SalesApp","sessionstate":"Actived","ipaddress":"127.0.0.1","serveripaddress":"127.0.0.1:5000","duration":101,"createtime":"2021-07-27T03:33:52.2616578Z","lastvisittime":"2021-07-27T03:33:52.6032097Z"}]
//-------------------------------------KillByID-----------------------------------------------

httpclient  lhc_client
string ls_url
string ls_sessionid

lhc_client = create httpclient

//GetSessions
//lhc_client.SetRequestHeader("Authorization", $token, true)  //If authentication is enabled
ls_url = "http://localhost:5000/api/Session/KillById"
//This URL should be replaced with the actual IP address and port number of PowerServer Web APIs

ls_sessionid = "8e3f5c6d-7515-4377-9a45-0e3349fcbfd2"
ls_url += "/"+ls_sessionid

lhc_client.sendrequest("post",ls_url)

if lhc_client.getresponsestatuscode() = 200 then
                messagebox("succeed",ls_sessionid +" was killed")
end if

//-------------------------------------------------------------------------------------------------------------------