Search code examples
c#azure-functionsmicrosoft-graph-apimicrosoft-graph-sdksazure-function-app

Validating JWT token obtained from Microsoft Graph API in HTTP-triggered functions


I was able to obtain a JWT token successfully using MSAL pattern from Graph API per the following code snippet below in C#:

app = ConfidentialClientApplicationBuilder.Create(_authenticationSettings.ClientId)
                    .WithClientSecret(_authenticationSettings.ClientSecret)
                    .WithAuthority(new Uri(_authenticationSettings.Authority))
                    .Build();
var result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
return result.AccessToken; 

I am also able to include this token in POST or GET requests' headers as a "Bearer" token to submit to an HTTP-triggered function. How can I validate this token in the function side to ensure it's authentic? All examples online are covering asp.net core applications and I could not find anything relevant to functions.


Solution

  • Here you can find a tutorial how to validate Graph API token in Azure Functions.

    JwtSecurityTokenHandler class is used for validating JWT.