Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE

How to list users with NPrinting APIs (PowerShell / Windows authentication)

No ratings
cancel
Showing results for 
Search instead for 
Did you mean: 
Sonja_Bauernfeind
Digital Support
Digital Support

How to list users with NPrinting APIs (PowerShell / Windows authentication)

Last Update:

Apr 13, 2021 9:53:53 AM

Updated By:

Sonja_Bauernfeind

Created date:

Apr 13, 2021 9:53:53 AM

This is a sample of how to call the NPrinting API to list users with PowerShell/Windows authentication.

 

Environment

 

Resolution

 

In order to call NPrinting API with Windows authentication, the first step is to call GET api/v1/login/ntlm to get a cookie, you will need to be logged in as a domain user that has proper privileges in NPrinting to run the following PowerShell script.

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$hdrs = @{}
$url = "https://servername:4993/api/v1/login/ntlm"
$resp = Invoke-WebRequest -Uri $url -Method Get -ContentType 'application/json' -Headers $hdrs -UseDefaultCredentials
$substring= [Regex]::Matches($resp.RawContent, "(?<=SameSite=None,NPWEBCONSOLE_SESSION\=).+?(?=; Path)")
$substring2= [Regex]::Matches($resp.RawContent, "(?<=NPWEBCONSOLE_XSRF-TOKEN\=).+?(?=; Path)")

#Create session
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$cookie = New-Object System.Net.Cookie     
$cookie.Name = "NPWEBCONSOLE_SESSION"
$cookie.Value = $substring.value
$cookie.Domain = "servername"
$session.Cookies.Add($cookie);

$hdrs.add("X-XSRF-TOKEN",$substring2.value)
$url = "https://servername:4993/api/v1/users"
Invoke-RestMethod -Uri $url -Method Get -ContentType 'application/json' -Headers $hdrs -WebSession $session

 

Notes:

For Nprinting versions prior to April 2020 Patch 1, the following line:

$substring= [Regex]::Matches($resp.RawContent, "(?<=SameSite=None,NPWEBCONSOLE_SESSION\=).+?(?=; Path)")

  will need to be:

$substring= [Regex]::Matches($resp.RawContent, "(?<=Secure,NPWEBCONSOLE_SESSION\=).+?(?=; Path)")

 


Regarding CSRF protection:

CSRF protection is needed on POST/PUT/PATCH calls (considered calls that modify data). In the sample above, that is done with the following 2 lines:

$substring2= [Regex]::Matches($resp.RawContent, "(?<=NPWEBCONSOLE_XSRF-TOKEN\=).+?(?=; Path)")
....
$hdrs.add("X-XSRF-TOKEN",$substring2.value)


As an alternative, the "Origin" header can also be used instead of the X-XSRF-TOKEN header, for example:

$hdrs.add("Origin","https://qlikserver2.domain.local")


The origin needs to be added in Trusted Origins in the "Settings" > "On-Demand" section of the NPrinting console.

Labels (1)
Version history
Last update:
‎2021-04-13 09:53 AM
Updated by: