From 797b307ecffdbf4d381cf147d92bf98553219ad9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9rald=20Barr=C3=A9?= Date: Tue, 4 Jun 2024 11:39:27 -0400 Subject: [PATCH] JWT token improvement (#41) * Changed token Signed-off-by: Paolo Salvatori * Fixed problem with JWT missing resource and expires_on Signed-off-by: Paolo Salvatori --------- Signed-off-by: Paolo Salvatori Co-authored-by: Paolo Salvatori --- Program.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Program.cs b/Program.cs index fcf734f..6a1cb6b 100644 --- a/Program.cs +++ b/Program.cs @@ -7,10 +7,19 @@ // Can be consumed by ManagedIdentityCredential by specifying IDENTITY_ENDPOINT and IMDS_ENDPOINT environment variables to this action URL // See https://github.com/Azure/azure-sdk-for-net/blob/Azure.Identity_1.8.0/sdk/identity/Azure.Identity/src/AzureArcManagedIdentitySource.cs -app.MapGet("/token", async (string resource) => + +app.MapGet("/token", async (HttpContext context, string resource) => { var token = await tokenCredential.GetTokenAsync(new TokenRequestContext(new[] { resource })); - return new Dictionary { ["access_token"] = token.Token, ["expires_on"] = token.ExpiresOn.ToString("O", CultureInfo.InvariantCulture) }; + context.Response.Headers.Add("Content-Type", "application/json"); // Set the Content-Type header to JSON + return new Dictionary + { + ["access_token"] = token.Token, + ["expiresOn"] = token.ExpiresOn.ToString("O", CultureInfo.InvariantCulture), + ["expires_on"] = token.ExpiresOn.ToUnixTimeSeconds().ToString(), + ["tokenType"] = "Bearer", + ["resource"] = resource + }; }); app.Run(); \ No newline at end of file