@@ -23,6 +23,7 @@ public class TableauConnectionHelper : ITableauConnectionHelperService
2323 {
2424 private readonly string connectedAppClientName ;
2525 private readonly string connectedAppSecretKey ;
26+ private readonly string connectedAppSecretId ;
2627 private readonly string connectedAppClientId ;
2728 private readonly string tableauUrl ;
2829 private readonly string dashboardUrl ;
@@ -31,6 +32,7 @@ public TableauConnectionHelper(IConfiguration config)
3132 {
3233 connectedAppClientName = config . GetTableauClientName ( ) ;
3334 connectedAppClientId = config . GetTableauClientId ( ) ;
35+ connectedAppSecretId = config . GetTableauClientSecretId ( ) ;
3436 connectedAppSecretKey = config . GetTableauClientSecret ( ) ;
3537 tableauUrl = config . GetTableauSiteUrl ( ) ;
3638 dashboardUrl = config . GetTableauDashboardUrl ( ) ;
@@ -39,27 +41,32 @@ public TableauConnectionHelper(IConfiguration config)
3941 public string GetTableauJwt ( string email )
4042 {
4143 var tokenHandler = new JwtSecurityTokenHandler ( ) ;
42- var securityKey = new SymmetricSecurityKey ( Encoding . UTF8 . GetBytes ( connectedAppSecretKey ) ) ;
43- var credentials = new SigningCredentials ( securityKey , SecurityAlgorithms . HmacSha256 ) ;
44+ var key = Encoding . ASCII . GetBytes ( connectedAppSecretKey ) ;
45+
4446 var claims = new [ ]
4547 {
4648 new Claim ( JwtRegisteredClaimNames . Sub , user ) ,
47- new Claim ( JwtRegisteredClaimNames . Iss , connectedAppClientId ) ,
48- new Claim ( "scp" , "tableau:views:embed" ) ,
49- new Claim ( "users.primaryemail" , email ) ,
5049 new Claim ( JwtRegisteredClaimNames . Jti , Guid . NewGuid ( ) . ToString ( ) ) ,
51- new Claim ( JwtRegisteredClaimNames . Exp ,
52- new DateTimeOffset ( DateTime . UtcNow . AddMinutes ( 20 ) ) . ToUnixTimeSeconds ( ) . ToString ( ) )
50+ new Claim ( "users.primaryemail" , email ) ,
51+ new Claim ( "scp" , "tableau:views:embed" )
5352 } ;
53+ var securityKey = new SymmetricSecurityKey ( key ) ;
54+ var credentials = new SigningCredentials ( securityKey , SecurityAlgorithms . HmacSha256Signature ) ;
55+ var header = new JwtHeader ( credentials ) ;
56+ header [ "kid" ] = connectedAppSecretId ; // Secret ID
57+ header [ "iss" ] = connectedAppClientId ; // Issuer (iss)
58+ var payload = new JwtPayload (
59+ connectedAppClientId , // Issuer (iss)
60+ "tableau" , // Audience (aud)
61+ claims ,
62+ notBefore : DateTime . UtcNow ,
63+ expires : DateTime . UtcNow . AddMinutes ( 5 )
64+ ) ;
5465
55- var token = new JwtSecurityToken (
56- issuer : connectedAppClientId ,
57- audience : "tableau" ,
58- claims : claims ,
59- expires : DateTime . UtcNow . AddMinutes ( 20 ) ,
60- signingCredentials : credentials ) ;
66+ var token = new JwtSecurityToken ( header , payload ) ;
67+ var tokenString = tokenHandler . WriteToken ( token ) ;
6168
62- return new JwtSecurityTokenHandler ( ) . WriteToken ( token ) ;
69+ return tokenString ;
6370 }
6471
6572 public async Task < string > AuthenticateUserAsync ( string jwtToken )
0 commit comments