ramansingh89
04-12-2024ETL
JWT Token with Iframe
Hi Team I am trying to find out a doc that can explain how JWT token works with iframe? If there is any code snippet to generate JWT token exists somewhere, please let me know as well
I haven't come across any documentation specific to what you're asking for here. But it would have been nice when I went this route, so there's definitely a need for it.
When I embedded a Sisense dashboard into an iFrame, I first set the iFrames SRC to the instance URL with the JWT token as part of the query string: i.e. https://path.toSisenseInstance.com/jwt?jwt=YOUR_TOKEN_HERE
In the iFrames OnLoad handler, I navigate the frame to the dashboard to be displayed. So essentially what happens is you hit the page, the iFrame makes its first call to the server with your token which authenticates it. Once that page loads, OnLoad is fired, which loads up the dashboard in the iFrame now that you're authenticated.
There's lots of examples for creating a JWT token online but here's a quick one. You'd just take this string value and place it in the query string that you first navigate to.
private string GenerateSisenseAuthToken()
{
string sharedKey = GetSharedSecret(); // This is the shared key configured in Sisense
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(sharedKey));
var credentials = new SigningCredentials(securityKey, "HS256");
var header = new JwtHeader(credentials);
var user = GetUserName(); // [email protected]
TimeSpan timeSinceEpoch = (_dateTimeProvider.UtcNow - new DateTime(1970, 1, 1));
int secondsSinceEpoch = (int)timeSinceEpoch.TotalSeconds;
var payload = new JwtPayload
{
{ "iat", secondsSinceEpoch},
{ "sub", user },
{ "jti", Guid.NewGuid().ToString() }
};
var secToken = new JwtSecurityToken(header, payload);
var handler = new JwtSecurityTokenHandler();
return handler.WriteToken(secToken);
}