Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
I am writing my own API and one of its endpoints needs to fetch data from a qlik app. My users authenticate against my API using a JWT token. I configured a virtual proxy in qlik to connect to qlik using the same token. This works, but only once per token. This is my code:
private string GetTokenFromAuthHeader()
{
string authorizationHeader = HttpContext.Request.Headers.Authorization;
if (string.IsNullOrEmpty(authorizationHeader) || !authorizationHeader.StartsWith("Bearer "))
{
throw new InvalidParameterValueException("No bearer token found");
}
return authorizationHeader.Substring("Bearer ".Length).Trim();
}
private static ILocation ConnectToQlikUsingToken(string token)
{
ILocation location = Location.FromUri($"wss://{server}");
location.VirtualProxyPath = "myproxy";
location.AsJwtViaProxy(token, false);
return location;
}
[HttpPost(Name = "myEndpoint")]
public IActionResult Post()
{
string token = GetTokenFromAuthHeader();
var location = ConnectToQlikUsingToken(token);
string appName = "my-appname";
var appIdentifier = LocationExtensions.AppWithNameOrDefault(location, appName);
Qlik.Engine.ISession session = Session.WithApp(appIdentifier, SessionType.Default);
var app = location.App(appIdentifier, session);
var result = app.EvaluateEx("=Count(Task.Category)");
return Ok(result);
}
Everything works well for the first request. But when I send another request to my API, authentication fails throwing this error:
System.Security.Authentication.AuthenticationException: Authentication failed.
at Qlik.Engine.Communication.QlikConnection.AwaitResponseTask[T](T task, String methodName, CancellationToken cancellationToken)
at Qlik.Engine.Communication.QlikConnection.AwaitResponse(Task task, String methodName, CancellationToken cancellationToken)
at Qlik.Engine.Communication.QlikConnection.Ping()
at Qlik.Sense.JsonRpc.GenericLocation.DisposeOnError(IDisposable o, Action f)
at Qlik.Engine.LocationExtensions.Hub(ILocation location, ISession session)
at Qlik.Engine.LocationExtensions.AppsWithNameOrDefault(ILocation location, String appName)
at Qlik.Engine.LocationExtensions.AppWithNameOrDefault(ILocation location, String appName)
Obtaining a new JWT token and using this works. Why? Do I need to close the session somehow? I tried Dispose() on the location and app but it does not help. Any ideas?