Rest API concurrent sessions usage

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Rest API concurrent sessions usage

L2 Linker

Hello Community,

I am looking to get help with using concurrent sessions feature of the the Rest API in Powershell 7 . I am able to produce multiple authentication tokens by setting the concurrentSessions parameter to True but I get an error when I pass these auth tokens to account search API URL using the ForEach-Object -Parallel feature in PS7. Here is my PS code:

1..2 | ForEach-Object -Parallel {

$lbody = "{`n`"concurrentSessions`": `"true`"`n}"

$LoginrequestTokens = Invoke-RestMethod https://<hostname>/PasswordVault/API/auth/Windows/Logon -UseDefaultCredentials -Method Post -Body $lbody

Write-Host $LoginrequestTokens

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"

$headers.Add("Content-Type", "application/json")

$headers.Add("Authorization", $LoginrequestTokens)

$response = Invoke-RestMethod 'https://<hostname>/PasswordVault/api/Accounts?search=<keyword>&searchType=contains&limit=1' -Method 'GET' -Headers $headers

$id=$response.value.id

WRITE-HOST $id

$Logoff = Invoke-RestMethod https://<hostname>/PasswordVault/API/auth/Logoff -Method POST -Headers $headers

Write-Host $Logoff

} -ThrottleLimit 2

 

 

Here is the error:

YWU5NTkwN2UtMTY4OC00ZDlmLTgyZTAtZThkYzljNTM0ZTBhO0ZBMDdBODFCMEM0N0QwMEI7MDAwMDAwMDIxMzA4QUEwNjUyNjZBOUZFREUwMjhCMDU4NTMyM0I0NDc0NDc3NzE0Qzg0RTEzMUZDOEUxODIwRDExOTA3RDZFMDAwMDAwMDA7

ODU2NDA1NTQtYmU4My00NWQyLTlmOTctMzJlZGVlZjA4NzhkOzAxODgyNUNGQzBFQUQ4M0Q7MDAwMDAwMDJGMzc0NENFQjcxNTM2MTJFOEU2NDIwQTIzNTQ3OUVEOTQxQjQ2RDJCRDQ4MDZFRjMxMjU0MjU0QTJDRjM2RkI3MDAwMDAwMDA7

Invoke-RestMethod:

Line |

8 | … $response = Invoke-RestMethod 'https://<host>/PasswordVault/ap …

| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

| {"ErrorCode":"ITACM040S","ErrorMessage":"User was automatically logged off from Vault (Diagnostic information: 540).\n\nLogoff from the Vault and logon again."}

 

@{LogoffUrl=}

Invoke-RestMethod:

Line |

8 | … $response = Invoke-RestMethod 'https://<host>/PasswordVault/ap …

| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

| {"ErrorCode":"ITACM040S","ErrorMessage":"User was automatically logged off from Vault (Diagnostic information: 540).\n\nLogoff from the Vault and logon again."}

 

@{LogoffUrl=}

 

 

I think the issue here is usage of incorrect authentication token in subsequent API calls which is ending the session abruptly? Would appreciate some tips on how to handle multiple authentication tokens.

 

Thanks,

Pooja

6 REPLIES 6

L4 Transporter

Disregarding the parallel loop for now:

  1. do your commands run without issue independently? i.e. can you authenticate, provide the auth token and query accounts successfully?
  2. can you use the separate (concurrentSessions) auth tokens to get results successfully?

 

 

Hi Pete - See my answers below

  1. do your commands run without issue independently? i.e. can you authenticate, provide the auth token and query accounts successfully?
  • Yes. With no loop, I get the token and able to query accounts with it

$lbody = "{`n`"concurrentSessions`": `"true`"`n}"

    $LoginrequestTokens = Invoke-RestMethod https://<host>/PasswordVault/API/auth/Windows/Logon -UseDefaultCredentials -Method Post -Body $lbody

    Write-Host $LoginrequestTokens

    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"

    $headers.Add("Content-Type", "application/json")

    $headers.Add("Authorization", $LoginrequestTokens)

    $response = Invoke-RestMethod 'https://<host>/PasswordVault/api/Accounts?search=<keyword>searchType=contains&limit=1' -Method 'GET' -Headers $headers

    $id=$response.value.id

    WRITE-HOST $id

$Logoff = Invoke-RestMethod https://<host>/PasswordVault/API/auth/Logoff -Method POST -Headers $headers

Write-Host $Logoff

 

  1. can you use the separate (concurrentSessions) auth tokens to get results successfully?

Sorry I'm not a powershell expert, not sure how to test this quickly. I can try to get back to you tomorrow on this.

 

Thanks for your response!

 

 

Actually, on your 2nd question, do you mean the authentication tokens generated by the parallel loop are valid outside of the loop? Not sure how I would generate concurrent sessions without using parallel loop (or parallel logins)?

well, just run the commands sequentially and set relevant variables:

$LoginrequestToken1

$Header1

$LoginrequestToken2

$Header2

 

Then you can test commands using both auth tokens and confirm validity.

Hi @Pete Maan​  ,

Yes, the tokens in sequential commands work

So I presume using `-ThrottleLimit 1` also works?

 

Am not sure of your need for a parallel loop to complete a simple query of accounts, but if you just authenticated once outside of the loop, it would likely suffice.

i.e.

$lbody = "{`n`"concurrentSessions`": `"true`"`n}"   $LoginrequestTokens = Invoke-RestMethod https://<hostname>/PasswordVault/API/auth/Windows/Logon -UseDefaultCredentials -Method Post -Body $lbody   Write-Host $LoginrequestTokens   $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"   $headers.Add("Content-Type", "application/json")   $headers.Add("Authorization", $LoginrequestTokens)   1..2 | ForEach-Object -Parallel {   $response = Invoke-RestMethod 'https://<hostname>/PasswordVault/api/Accounts?search=<keyword>&searchType=contains&limit=1' -Method 'GET' -Headers $($using:headers)   $id=$response.value.id   WRITE-HOST $id   } -ThrottleLimit 2   $Logoff = Invoke-RestMethod https://<hostname>/PasswordVault/API/auth/Logoff -Method POST -Headers $headers

Given the significant processing overhead this type of parallel loop has, and also the potential load this could introduce in terms of vault tasks depending on how your environment is configured, you should keep a close eye on performance - both of the script and the vault.

  • 1579 Views
  • 6 replies
  • 0 Likes
Like what you see?

Show your appreciation!

Click Like if a post is helpful to you or if you just want to show your support.

Click Accept as Solution to acknowledge that the answer to your question has been provided.

The button appears next to the replies on topics you’ve started. The member who gave the solution and all future visitors to this topic will appreciate it!

These simple actions take just seconds of your time, but go a long way in showing appreciation for community members and the LIVEcommunity as a whole!

The LIVEcommunity thanks you for your participation!