|
13 | 13 | using System.Net.Http.Headers; |
14 | 14 | using System.Net.Http; |
15 | 15 | using System.Threading.Tasks; |
| 16 | + using Microsoft.AspNetCore.DataProtection; |
16 | 17 |
|
17 | 18 | public interface ITableauConnectionHelperService |
18 | 19 | { |
@@ -40,32 +41,34 @@ public TableauConnectionHelper(IConfiguration config) |
40 | 41 | } |
41 | 42 | public string GetTableauJwt(string email) |
42 | 43 | { |
43 | | - var tokenHandler = new JwtSecurityTokenHandler(); |
44 | | - var key = Encoding.ASCII.GetBytes(connectedAppSecretKey); |
| 44 | + var key = Encoding.UTF8.GetBytes(connectedAppSecretKey); |
| 45 | + var signingCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256); |
45 | 46 |
|
46 | | - var claims = new[] |
47 | | - { |
48 | | - new Claim(JwtRegisteredClaimNames.Sub, user), |
49 | | - new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), |
50 | | - new Claim("users.primaryemail", email), |
51 | | - new Claim("scp", "tableau:views:embed") |
52 | | - }; |
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 | | - ); |
| 47 | + var claims = new List<Claim> |
| 48 | + { |
| 49 | + new Claim(JwtRegisteredClaimNames.Sub, user), |
| 50 | + new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), |
| 51 | + new Claim("users.primaryemail", email), |
| 52 | + }; |
| 53 | + |
| 54 | + var header = new JwtHeader(signingCredentials) |
| 55 | + { |
| 56 | + { "kid", connectedAppSecretId }, |
| 57 | + { "iss", connectedAppClientId } |
| 58 | + }; |
| 59 | + |
| 60 | + var payload = new JwtPayload |
| 61 | + { |
| 62 | + { "iss", connectedAppClientId }, |
| 63 | + { "aud", "tableau" }, |
| 64 | + { "exp", new DateTimeOffset(DateTime.UtcNow.AddMinutes(5)).ToUnixTimeSeconds() }, |
| 65 | + { "sub", user }, |
| 66 | + { "scp", new[] { "tableau:content:read" } } |
| 67 | + }; |
65 | 68 |
|
66 | 69 | var token = new JwtSecurityToken(header, payload); |
| 70 | + var tokenHandler = new JwtSecurityTokenHandler(); |
67 | 71 | var tokenString = tokenHandler.WriteToken(token); |
68 | | - |
69 | 72 | return tokenString; |
70 | 73 | } |
71 | 74 |
|
|
0 commit comments