Skip to content

Instantly share code, notes, and snippets.

@DhyanRathore
Last active March 19, 2021 15:17
Show Gist options
  • Save DhyanRathore/838849f4279b5592b2f16f862fc2b15d to your computer and use it in GitHub Desktop.
Save DhyanRathore/838849f4279b5592b2f16f862fc2b15d to your computer and use it in GitHub Desktop.
Get User Claims from the Authentication Tokens
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using System.Security.Claims;
using System.IdentityModel.Tokens.Jwt;
// DEMO: Get user Claims from the Authentication Tokens
namespace ClaimsDemo.Function
{
public static class ClaimsDemo
{
[FunctionName("ClaimsDemo")]
public static IActionResult Run
([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]
HttpRequest req, ILogger log)
{
//Extract AAD ID Token from the request headers
var userIDToken = req.Headers["X-MS-TOKEN-AAD-ID-TOKEN"];
log.LogInformation("Encrypted JWT: " + userIDToken);
//Read and decrypt the JWT
var jwttoken = new JwtSecurityTokenHandler().ReadJwtToken(userIDToken) as JwtSecurityToken;
log.LogInformation("Decrypted JWT:");
log.LogInformation("Claim Type : Claim Value");
//Extract the claims from the JWT
foreach (Claim claim in jwttoken.Claims)
{
log.LogInformation(claim.Type + " : " + claim.Value + "\n");
}
return new OkObjectResult("Success");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment