Select Page

I was recently tasked with pulling the health check information from the Qlik Sense enterprise health check api. This involved authenticating a .Net client against the Qlik Sense Repository Service API.

There are two ways of authenticating against Qlik for .Net. The first one is to use certificate authentication, the second (and the one we are covering today) is to use the current windows account to authenticate. Both of there options are covered here.

Here is the code used to authenticate and query the API, very simple!

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

public string GetHealthJsonString() {
    string xrf = GetXrfKey();

    HttpWebRequest request =
        (HttpWebRequest) WebRequest.Create(
            $@"{QlikHostName}/engine/healthcheck?xrfkey=" + xrf
        );
    request.Method = "GET";
    request.UserAgent = "Windows";
    request.Accept = "application/json";
    request.Headers.Add("X-Qlik-xrfkey", xrf);
    // Specify we want to query as the current Windows user.
    request.UseDefaultCredentials = true;
    // Create the web request and return the content.
    HttpWebResponse response = (HttpWebResponse) request.GetResponse();
    Stream stream = response.GetResponseStream();
    if (stream == null) {
        throw new Exception("Failed to get valid response");
    }
    return new StreamReader(stream).ReadToEnd();
}

For clarity I will quickly break down what is happening here.

First we are creating a XrfKey which helps prevent request forgery. It is a simple random sequence of 16 chars that is shared in the query parameters and the request body.

string xrf = GetXrfKey();

Here is an example of how GetXrfKey works.

private static string GetXrfKey() {
    const int size = 16;
    const string chars = "abcdefghijklmnopqrstuwxyz" +
                         "ABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
    StringBuilder xrf = new StringBuilder();
    Random r = new Random();
    for (int i = 0; i < size; i++) {
        xrf.Append(chars[r.Next(0, chars.Length)]);
    }
    return xrf.ToString();
}

Next we are creating the HttpWebRequest object that we will use to query the health api with.

    HttpWebRequest request =
        (HttpWebRequest) WebRequest.Create(
            $@"{QlikHostName}/engine/healthcheck?xrfkey=" + xrf
        );

Then we set some headers and request properties.

request.Method = "GET"; // Method type to GET
request.UserAgent = "Windows"; // Use windows to auth
request.Accept = "application/json"; // Expecting json data.
request.Headers.Add("X-Qlik-xrfkey", xrf); // Add our xrf key

Next tell the request system that we want to use the current Windows user.

// Specify we want to query as the current Windows user.
request.UseDefaultCredentials = true;

Then we send the request and check that it returns something. If it does we have our data!

// Create the web request and return the content.
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
Stream stream = response.GetResponseStream();
if (stream == null) {
    throw new Exception("Failed to get valid response");
}
return new StreamReader(stream).ReadToEnd();

Hope that helps with your project.
Happy querying!

Coffee to Code
Timothy Gray – Code Conjuror 

 

Tim blogs about code that can make data consumable and visible.

To read all of our Blogs on Qlik Sense follow this link.

%d bloggers like this: