using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text.Json.Serialization;
static void Main(string[] args)
var idToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6IkwxS2ZLRklfam5YYndXYzIyeFp4dzFzVUhIMCJ9.eyJhdWQiOiJjNDRiNDA4My0zYmIwLTQ5YzEtYjQ3ZC05NzRlNTNjYmRmM2MiLCJpc3MiOiJodHRwczovL2xvZ2luLm1pY3Jvc29mdG9ubGluZS5jb20vNzYwY2Y5NjctODY4Ny00YzljLWIzYTUtMmNmMmM0ODhkYjdiL3YyLjAiLCJpYXQiOjE3MTUyMTQyNDcsIm5iZiI6MTcxNTIxNDI0NywiZXhwIjoxNzE1MjE4MTQ3LCJhaW8iOiJBV1FBbS84V0FBQUExeXZLT3FmblV2OFFlbi9XY1BsN0hody92cXc5cGMyV3R3aUkza0RtSmo1dk5RR2xGTjl4OVNTWld6dm5JQmxIcnhVaWVVaHArZXYzQnhQb3ZobnN5NjNkcnE5NytxdmRKTzRiQkoydStmeGZHS2JCeWw1Q3JhQlVBTjJzVC95WCIsImVtYWlsIjoiaXRfZGV2QG56d2FneXUuY29tIiwibmFtZSI6IklUIERldmVsb3BtZW50Iiwib2lkIjoiMjczYmYwNTQtZTQ5Yi00N2ExLWExMDAtMzUyOThlMTBkM2JlIiwicHJlZmVycmVkX3VzZXJuYW1lIjoiaXRfZGV2QG56d2FneXUuY29tIiwicHVpZCI6IjEwMDMyMDAyRkZDQjVDOUQiLCJyaCI6IjAuQVVFQVpfa01kb2VHbkV5enBTenl4SWpiZTROQVM4U3dPOEZKdEgyWFRsUEwzenhCQUU0LiIsInNpZCI6IjQ4NWY5YTQ2LTgwNTktNGRiZS05NDUyLWMzM2JhYzVjMzUyZiIsInN1YiI6IlpZYVdQNWtPVGF6dWJjZVpuSUhOTUNFWGRUUFd3WkFVeTU1VEtEeGRFMXciLCJ0ZW5hbnRfcmVnaW9uX3Njb3BlIjoiT0MiLCJ0aWQiOiI3NjBjZjk2Ny04Njg3LTRjOWMtYjNhNS0yY2YyYzQ4OGRiN2IiLCJ1cG4iOiJpdF9kZXZAbnp3YWd5dS5jb20iLCJ1dGkiOiJtM0R4djBrSFNrdWJMN0JENEhVbkFBIiwidmVyIjoiMi4wIiwieG1zX21wY2kiOjI1OTIwMCwieG1zX3BjaSI6MzYwMH0.GPiUI1mxO1xSiqAFlcZtknpUkwIxdI7uxvjD7t4uwkxAAJTRRLM-XshQGsECZEhQIe0BwN8ePekeY-LIVOAsw5XP8Dyz3lMhqX_T0sCpowEZonb23iMAKfYB0-osRS9Q1ok6RwKcY4o2jfU2IntXnJe1E9DePuOyMJ0DAVRhk3WmXwU1mX4eaxMPjUNn9ACkZ6aVqdudXeK5i4hz3eMXWAyvCLsuTslWuKUc9OvaYzN8gs41_E4Xq-d37OIljJtBFUxjflMbkBBVlCGNwHYKVq9htgmK31GC5Y7mVXMRD6QHJTz_N9GXVC6FYM26pvgNjS9p2MOA389xg0a-UZwtnA";
var tokenService = new TokenService();
var decodedToken = tokenService.DecodeToken(idToken);
Console.WriteLine($"Token decoded: {decodedToken.Upn}");
public class TokenService
public TokenDecoded? DecodeToken(string token)
string[] parts = token.Split('.');
if (parts.Length <= 0) return null!;
string payloadJson = DecodeBase64Url(parts[1]);
return JsonSerializer.Deserialize<TokenDecoded>(payloadJson)!;
private static string DecodeBase64Url(string base64Url)
string base64 = base64Url.Replace('-', '+').Replace('_', '/');
byte[] bytes = Convert.FromBase64String(base64);
return Encoding.UTF8.GetString(bytes);
public class TokenDecoded
[JsonPropertyName("aud")]
public string Aud { get; set; } = null!;
[JsonPropertyName("iss")]
public string Iss { get; set; } = null!;
[JsonPropertyName("iat")]
public int? Iat { get; set; }
[JsonPropertyName("nbf")]
public int? Nbf { get; set; }
[JsonPropertyName("exp")]
public long Exp { get; set; }
[JsonPropertyName("acct")]
public int? Acct { get; set; }
[JsonPropertyName("acr")]
public string Acr { get; set; } = null!;
[JsonPropertyName("aio")]
public string Aio { get; set; } = null!;
[JsonPropertyName("amr")]
public List<string> Amr { get; set; } = null!;
[JsonPropertyName("app_displayname")]
public string AppDisplayname { get; set; } = null!;
[JsonPropertyName("appid")]
public string Appid { get; set; } = null!;
[JsonPropertyName("appidacr")]
public string Appidacr { get; set; } = null!;
[JsonPropertyName("idtyp")]
public string Idtyp { get; set; } = null!;
[JsonPropertyName("ipaddr")]
public string Ipaddr { get; set; } = null!;
[JsonPropertyName("name")]
public string Name { get; set; } = null!;
[JsonPropertyName("oid")]
public string Oid { get; set; } = null!;
[JsonPropertyName("platf")]
public string Platf { get; set; } = null!;
[JsonPropertyName("puid")]
public string Puid { get; set; } = null!;
public string Rh { get; set; } = null!;
[JsonPropertyName("scp")]
public string Scp { get; set; } = null!;
[JsonPropertyName("sub")]
public string Sub { get; set; } = null!;
[JsonPropertyName("tenant_region_scope")]
public string TenantRegionScope { get; set; } = null!;
[JsonPropertyName("tid")]
public string Tid { get; set; } = null!;
[JsonPropertyName("unique_name")]
public string UniqueName { get; set; } = null!;
[JsonPropertyName("upn")]
public string Upn { get; set; } = null!;
[JsonPropertyName("uti")]
public string Uti { get; set; } = null!;