How to disconnect or log off a session in Citrix DaaS
Use REST APIs to to disconnect or log off a session from your Citrix DaaS (formerly Citrix Virtual Apps and Desktops service) site.
Follow the prerequisites and examples to get started with this API.
You can make API requests using the PowerShell code, C# code, Python, or any tool that supports invoking the REST API.
Prerequisites to disconnect or log off a session
- Read the Get started with Citrix Cloud APIs section to ensure that you have the
bearer token
. - Get
siteid
from How to get site id API. - Get
id
of the session to be disconnected or logged off from How to perform an advanced search for sessions API. - Make sure that
Citrix Connector
has been installed in your domain service. - Invoke the API described in this document from a client host or from the API exploration tab to disconnect or log off a virtual app or desktop session.
Disconnect or log off a session from your site using any REST API tool
Learn from the following example to disconnect or log off a virtual app or desktop session from your Citrix DaaS site using any REST API tool.
Disconnecting a session followed by a manual reconnect helps to solve some temporary session issues, for example, graphics corruption.
Logging off the session helps to recover from session-level issues, for example, application or desktop hang. It can also be used for security reasons.
The time needed to completely log off a Windows session is unpredictable. Therefore, response to the session log off API is asyncronous. The response has a job URL in the Location
header, which can then be queried for the status and result of the log off process.
To avoid data loss, it is recommended that the session owners be notified to save their work before the session disconnects or logs off.
Request for session disconnect
POST https://api.cloud.com/cvad/manage/Sessions/{id}/$disconnect HTTP/1.1
Accept: application/json
Content-Type: application/json; charset=utf-8
Authorization: CWSAuth bearer=<token-from-prerequisites>
Citrix-CustomerId: loy6oujtu6a4
Citrix-InstanceId: 22ded57c-0306-47e4-b6e8-fed6252759e1
<!--NeedCopy-->
Response for session disconnect
HTTP/1.1 200 OK
citrix-transactionid: d4cbb328-c3c1-476e-8505-25890bf88155
content-Length: 8320
content-Type: application/json; charset=utf-8
date: "Tue, 08 Dec 2020 11:34:57 GMT"
{
"Items": [
{
"Id": "41ba32e9-d12b-485c-afd2-c4d0d047cd8b",
"Uid": 8,
"ApplicationsInUse": [
{
"Id": "00000000-0000-0000-0000-000000000000",
"Uid": null,
"Name": "WorkingApp_1"
}
],
"AppState": "Active",
"AppStateLastChangeTime": "11/16/2020 2:01:04 PM",
"Brokering": {
"AutonomouslyBrokered": false,
"DurationMilliseconds": null,
"Time": null,
"UserName": null,
"UserSid": null
},
"Client": {
"DeviceId": null,
"HardwareId": "55667788",
"IPAddress": "10.107.1.120",
"Name": "",
"Platform": "Unknown",
"ProductId": 9,
"ReceiverIPAddress": null,
"ReceiverName": null,
"Version": "v1.0"
},
"Connection": {
"ConnectedViaHostName": null,
"ConnectedViaIP": "10.107.1.34",
"ConnectionMode": "Brokered",
"LaunchedViaHostName": null,
"LaunchedViaIP": "20.62.252.155",
"Protocol": "Hdx",
"SecureIcaActive": false,
"SmartAccessTags": []
},
"ContainerScopes": [
{
"Scopes": [
{
"Id": "00000000-0000-0000-0000-000000000000",
"Uid": null,
"Name": "All",
"Description": null,
"IsBuiltIn": true,
"IsAllScope": true,
"IsTenantScope": false,
"TenantId": null,
"TenantName": null
}
],
"ScopeType": "MachineCatalog"
},
{
"Scopes": [
{
"Id": "00000000-0000-0000-0000-000000000000",
"Uid": null,
"Name": "All",
"Description": null,
"IsBuiltIn": true,
"IsAllScope": true,
"IsTenantScope": false,
"TenantId": null,
"TenantName": null
}
],
"ScopeType": "DeliveryGroup"
}
],
"EstablishmentDurationMilliseconds": 22260,
"EstablishmentTime": "11/16/2020 2:01:04 PM",
"IsAnonymousUser": false,
"IsHidden": false,
"LogoffInProgress": false,
"LogonInProgress": false,
"Machine": {
"Id": null,
"Uid": 15,
"AgentVersion": "1912.0.1000.24525",
"AllocationType": "Random",
"ApplicationsInUse": null,
"AssociatedUsers": null,
"MachineCatalog": {
"Id": "0520987c-0312-4e1c-bd09-7714fc8732ec",
"Uid": 2,
"Name": "Sales-B"
},
"ContainerScopes": null,
"ControllerDnsName": null,
"DeliveryGroup": {
"Id": "5c796610-cb80-40ae-8378-5cbd5cefbd89",
"Uid": 2,
"Name": "Sales"
},
"DeliveryType": null,
"Description": null,
"DesktopConditions": null,
"DnsName": "Sales-B03.htlabs04.example.com",
"Hosting": {
"HostedMachineId": null,
"HostedMachineName": null,
"HostingServerName": null,
"LastHostingUpdateTime": null,
"HypervisorConnection": {
"Id": "00000000-0000-0000-0000-000000000000",
"Uid": null,
"Name": "SalesVda"
},
"ImageOutOfDate": false
},
"InMaintenanceMode": false,
"IPAddress": "10.107.128.208",
"IsAssigned": null,
"MachineType": "Virtual",
"LastConnectionFailure": null,
"LastConnectionTime": null,
"LastConnectionUser": null,
"LastDeregistrationReason": null,
"LastDeregistrationTime": null,
"LastErrorReason": null,
"LastErrorTime": null,
"LoadIndex": null,
"Name": "HTLABS04\\Sales-B03",
"OSType": "Windows 2019",
"OSVersion": null,
"PersistUserChanges": "Discard",
"PowerActionPending": null,
"PowerState": "Unknown",
"ProvisioningType": "MCS",
"PublishedApplications": null,
"RegistrationState": null,
"ScheduledReboot": "Unknown",
"SessionClientAddress": null,
"SessionClientName": null,
"SessionClientVersion": null,
"SessionConnectedViaHostName": null,
"SessionConnectedViaIP": null,
"SessionCount": null,
"SessionLaunchedViaHostName": null,
"SessionLaunchedViaIP": null,
"SessionProtocol": null,
"SessionSecureIcaActive": null,
"SessionSmartAccessTags": null,
"SessionStartTime": null,
"SessionState": null,
"SessionStateChangeTime": null,
"SessionSupport": "SingleSession",
"SessionUserName": null,
"Sid": null,
"SummaryState": "Disconnected",
"WillShutdownAfterUse": null,
"WindowsConnectionSetting": null,
"Zone": {
"Id": "1d405094-3abe-4ab9-b304-b21522bcd3e6",
"Uid": null,
"Name": "htlabs04-EastUs"
},
"SupportedPowerActions": null,
"FaultState": "Unknown"
},
"SessionType": "Application",
"StartTime": "11/16/2020 2:01:04 PM",
"State": "Disconnected",
"StateChangeTime": "11/16/2020 2:02:49 PM",
"UntrustedUserName": "HTLABS04\\DevUser1",
"User": {
"DisplayName": "DevUser1",
"SamName": null,
"SamAccountName": null,
"CommonName": null,
"PrincipalName": "DevUser1@htlabs04.example.com",
"Sid": "S-1-5-21-886277602-2428626936-2556752729-2162",
"GroupSids": null,
"DenyOnlySids": null,
"PasswordCanExpire": null,
"DaysUntilPasswordExpiry": null,
"IsBuiltIn": null,
"IsGroup": null,
"Name": "HTLABS04\\DevUser1",
"StreetAddress": null,
"City": null,
"State": null,
"Country": null,
"Mail": null,
"TelephoneNumber": null,
"HomePhone": null,
"Mobile": null,
"Enabled": null,
"Locked": null,
"PropertiesFetched": 0,
"Domain": null,
"Forest": null,
"Guid": null,
"DistinguishedName": null,
"CanonicalName": null,
"PossibleLookupFailure": false,
"DirectoryServer": null
}
}
],
"ContinuationToken": null,
"TotalItems": 1
}
<!--NeedCopy-->
Request for session log off
POST https://api.cloud.com/cvad/manage/Sessions/{id}/$logoff HTTP/1.1
Accept: application/json
Content-Type: application/json; charset=utf-8
Authorization: CWSAuth bearer=<token-from-prerequisites>
Citrix-CustomerId: loy6oujtu6a4
<!--NeedCopy-->
Response for session log off
HTTP/1.1 200 OK
citrix-transactionid: ce0d3943-e294-4fc8-b2c6-85271d4a0c3b
content-Type: application/json; charset=utf-8
date: "Tue, 08 Dec 2020 11:34:57 GMT"
Location: https://api.cloud.com/cvad/manage/Jobs/81c05d9f...
<!--NeedCopy-->
Disconnect or log off a session from your site using PowerShell
Learn from the following example to disconnect or log off a virtual app or desktop session from your Citrix DaaS site using any PowerShell code.
function DisconnectSession {
param (
[Parameter(Mandatory=$true)]
[string] $customerid,
[Parameter(Mandatory=$true)]
[string] $siteid,
[Parameter(Mandatory=$true)]
[string] $sessionid,
[Parameter(Mandatory=$true)]
[string] $bearerToken
)
$requestUri = [string]::Format("https://api.cloud.com/cvad/manage/Sessions/{0}/`$disconnect", $sessionid)
$headers = @{
"Accept" = "application/json";
"Authorization" = "CWSAuth Bearer=$bearerToken";
"Citrix-CustomerId" = $customerid;
"Citrix-InstanceId" = $siteid;
}
$response = Invoke-RestMethod -Uri $requestUri -Method POST -Headers $headers
return $response
}
function LogoffSession {
param (
[Parameter(Mandatory=$true)]
[string] $customerid,
[Parameter(Mandatory=$true)]
[string] $siteid,
[Parameter(Mandatory=$true)]
[string] $sessionid,
[Parameter(Mandatory=$true)]
[string] $bearerToken
)
$requestUri = [string]::Format("https://api.cloud.com/cvad/manage/Sessions/{0}/`$logoff", $sessionid)
$headers = @{
"Accept" = "application/json";
"Authorization" = "CWSAuth Bearer=$bearerToken";
"Citrix-CustomerId" = $customerid;
"Citrix-InstanceId" = $siteid;
}
$response = Invoke-RestMethod -Uri $requestUri -Method POST -Headers $headers
return $response
}
$customerId = "customer1"
$siteId = "61603f15-cdf9-4c7f-99ff-91636601a795"
$sessionId = "56f1cbf3-1cc6-40cd-9c82-c95633ba88bb"
$bearerToken = "ey1.."
$response = DisconnectSession $customerid $siteid $sessionId $bearerToken
$response = LogoffSession $customerid $siteid $sessionId $bearerToken
$resultlocation = $response.Headers["Location"]
<!--NeedCopy-->
Disconnect or log off a session from your site using C# code
Learn from the following example to disconnect or log off a virtual app or desktop session from your Citrix DaaS site using any C# code.
public static async Task<string> DisconnectSession(
string customerid,
string siteid,
string bearerToken,
string sessionid)
{
var requestUri = "https://api.cloud.com/cvad/manage/Sessions/{0}/$disconnect", sessionid);
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.ParseAdd("application/json");
client.DefaultRequestHeaders.Add("Citrix-CustomerId", customerid);
client.DefaultRequestHeaders.Add("Citrix-InstanceId", siteid);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("CWSAuth Bearer=" + bearerToken);
var jsonBody = JsonConvert.SerializeObject(model, new JsonSerializerSettings
{
Converters = new JsonConverter[] { new StringEnumConverter() }
});
var response = await client.PostAsync(requestUri, new StringContent(jsonBody, Encoding.UTF8, "application/json"));
if (response != null)
{
var content = await response.Content.ReadAsStringAsync();
return content;
}
return null;
}
}
public static async Task<string> LogoffSession(
string customerid,
string siteid,
string bearerToken,
string sessionid)
{
var requestUri = string.Format("https://api.cloud.com/cvad/manage/Sessions/{1}/$logoff", siteid, sessionid);
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.ParseAdd("application/json");
client.DefaultRequestHeaders.Add("Citrix-CustomerId", customerid);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("CWSAuth Bearer=" + bearerToken);
var response = await client.PostAsync(requestUri, null);
if (response != null)
{
var content = await response.Content.ReadAsStringAsync();
return content;
}
return null;
}
}
<!--NeedCopy-->
Disconnect or log off a session from your site using Python
Learn from the following example to disconnect or log off a virtual app or desktop session from your Citrix DaaS site using Python.
import requests
def DisconnectSession(bearerToken, customerid, sessionid, siteid):
request_uri = "https://api.cloud.com/cvad/manage/Sessions/{0}/$disconnect".format(sessionid)
headers = {
'Authorization': 'CWSAuth Bearer=%s' % bearerToken,
'Citrix-CustomerId': customerid,
'Citrix-InstanceId': siteid,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
response = requests.post(request_uri, headers = headers)
return response.json()
def LogoffSession(bearerToken, customerid, sessionid, siteid):
request_uri = "https://api.cloud.com/cvad/manage/Sessions/{0}/$logoff".format(sessionid)
headers = {
'Authorization': 'CWSAuth Bearer=%s' % bearerToken,
'Citrix-CustomerId': customerid,
'Citrix-InstanceId': siteid,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
response = requests.post(request_uri, headers = headers)
return response.json()
<!--NeedCopy-->
In this article
- Prerequisites to disconnect or log off a session
- Disconnect or log off a session from your site using any REST API tool
- Disconnect or log off a session from your site using PowerShell
- Disconnect or log off a session from your site using C# code
- Disconnect or log off a session from your site using Python