Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
UmeshC
Contributor
Contributor

Using .NET SDK (C#) to connect to QlikSense using a web proxy server

I have used QlikEngine JSON API and exploring how to achieve similar results with the .NET SDK. I have a few questions based on the code snippet below:

 

Uri uri = new Uri("wss://my_remote_host/jwt/app?transient="); 
//virtual proxy
            
AppIdentifier appId = new AppIdentifier();
appId.AppId = "<SOME APPID>";
appId.AppName = "<SOME APP NAME>";
var senseLocation = Location.FromUri(uri);

senseLocation.AsJwtViaProxy(Guid.NewGuid().ToString(), false); 
//hope I can pass a GUID for key
var bearerToken = GetToken(); 
//getting a bearer token to pass in the header
senseLocation.CustomUserHeaders.Add("Authorization", "bearer " + bearerToken); //passing authorization header

var senseSession = Session.WithApp(appId, SessionType.Default);
QlikConnection conn = new QlikConnection(senseLocation, senseSession);
IEnumerable<string> argNames = new List<string>(); //WHAT ARGS?
string[] arguments = null; //WHAT PARAMS?
var response = await conn.SendAsync(1, "GetDocList", argNames, arguments);

 

 

  1. Hope I can pass a unique value like a GUID string for the JWTProxy object (in senseLocation.AsJwtViaProxy(Guid.NewGuid().ToString(), false))
  2. Can the URL be same as the wss:// URL used for websocket JSON API?

  3. My connection requires a HTTP proxy as well. Where do I specify the same? (I have tested this is working for websocket JSON API). 

  4. How do I make the JSON payload and pass it as arguments to SendAsync method? The method requires a List<string> and a string[]. Need to know where/how to pass the JSON payload for the GetDocList server method. In the websocket JSON API, I would normally pass the JSON
    like {"jsonrpc":"2.0","id":0"handle":-1,"method":"GetDocList","params":[]}

  5. I tried below code to iterate over document list using the Hub but connection fails here as well.

 

            using (IHub hub = senseLocation.Hub()) 
            //SocketException: No such host is known
            {
                var apps = hub.GetAppList();
                foreach (var app in apps)
                {
                    Console.WriteLine(app.AppId + ": " + app.AppName);
                }
            }

 

Thanks a ton in advance.

1 Solution

Accepted Solutions
Øystein_Kolsrud
Employee
Employee

I'm not sure I understand what you mean by proxy in this context. Are you referring to something outside of Qlik Sense, or are you referring to a Qlik Sense Proxy as can be configured through the QMC?

In either case, you typically don't need to do any special configuration for the SDK connection. Just use the same url that you would use in your browser but without the virtual proxy path. That one you put in the "VirtualProxyPath" property for the location. Typically you don't define a port here either.

But you are referring to the "DefaultCredentials", does this mean that you want to log in with your default Windows credentials? If that is the case, then you should use "AsNtlmUserViaProxy" instead of "AsJwtViaProxy".

View solution in original post

10 Replies
Øystein_Kolsrud
Employee
Employee

When you use AsJwtViaProxy, the SDK will set the bearer token for you, so you don't have to explicitly set it. The "key" argument is the token. So a JWT connection typically looks like this:

 

var location = Location.FromUri("https://my_remote_host");
location.VirtualProxyPath = "jwt";
location.AsJwtViaProxy(key, false);
using (var hub = location.Hub())
{
    Console.WriteLine(hub.QTProduct());
}

 

I notice that you have "/jwt/app?transient=" as part of your URI, and that probably shouldn't be there.

You can use any of the schemes https, http, wss and ws. http and ws will only work if your virtual proxy allows it, but it sounds like you have already enabled that in your case.

You definitely don't want to pass a random GUID as the token, as this is the token that will be used to authenticate you. This page is a good place to go to learn how to generate a valid JWT token:

https://community.qlik.com/t5/Support-Knowledge-Base/Qlik-Sense-How-to-set-up-JWT-authentication/ta-...

As for calling the different endpoints, it is rather uncommon to work directly with QlikConnection objects and SendAsync. The code you list in bullet 5 looks fine, but you'll need to get your connection working first.

UmeshC
Contributor
Contributor
Author

Thanks a ton for the pointer on using the key argument to pass the token.
The other point I am missing is how to connect to the end point via a HTTP proxy.

If I did this for JSON API, the code would be like below. I have the opportunity to specify a proxy because I am the one creating the websocket object. 

TheSocket = new ClientWebSocket();
ICredentials credentials = CredentialCache.DefaultCredentials;
IWebProxy webProxy = new WebProxy("http://myproxy:port" , true);
webProxy.Credentials = credentials;
TheSocket.Options.Proxy = webProxy;

 

Thanks again.

Øystein_Kolsrud
Employee
Employee

I'm not sure I understand what you mean by proxy in this context. Are you referring to something outside of Qlik Sense, or are you referring to a Qlik Sense Proxy as can be configured through the QMC?

In either case, you typically don't need to do any special configuration for the SDK connection. Just use the same url that you would use in your browser but without the virtual proxy path. That one you put in the "VirtualProxyPath" property for the location. Typically you don't define a port here either.

But you are referring to the "DefaultCredentials", does this mean that you want to log in with your default Windows credentials? If that is the case, then you should use "AsNtlmUserViaProxy" instead of "AsJwtViaProxy".

UmeshC
Contributor
Contributor
Author

To clarify, both "proxies" are applicable in my implementation:

1. Yes there is a web proxy outside of Qlik. My client application (where I intend to use .NET SDK) cannot connect to Qlik servers directly, and therefore need to configure a connection proxy server that sits between the .NET SDK Client and Qlik servers.

2. I also have to use the QlikSense proxy configured using QMC. 

tts
Employee
Employee

So, is it possible specify Web proxy url when connecting QS server, using Qlik Sense .NET SDK?

The latest SDK still has no way to support Web proxy????

tts
Employee
Employee

For example, enigma-go module added support Web proxy setting as below.
https://github.com/qlik-oss/enigma-go/blob/91568aab445a0c6bd64afc702e9c2fc7b696b806/default_dialer.g...
--------
dialer.CreateSocket = func(ctx context.Context, url string, httpHeader http.Header) (Socket, error) {
gorillaDialer := websocket.Dialer{
Proxy: http.ProxyFromEnvironment, // Will pick the Proxy URL from the environment variables (HTTPS_PROXY).

--------

tts
Employee
Employee

>I'm not sure I understand what you mean by proxy in this context. Are you referring to something outside of Qlik Sense,

Do you understand Web proxy? For example, the web client program may need to use Web proxy systemm, using http://webproxy:88888, to access web server running in Internet space.
So, I guess that Qlik Sense .NET SDK assembly(Qlik.Sense.JsonRpc.dll) uses .NET's ClientWebSocket class to access Qlik Sense Engine with WebSocket. However, we can not set the Proxy property.
https://docs.microsoft.com/en-us/dotnet/api/system.net.websockets.clientwebsocketoptions.proxy?view=...

Øystein_Kolsrud
Employee
Employee

@UmeshCIf you use .NET Core 3.0 or later, then I believe you can use the default proxy setting of the HttpClient class to achieve this:

https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.defaultproxy?view=netcore-3.0

tts
Employee
Employee

Have you tested HttpClient.DefaultProxy Property with ClientWebSocket class?