using Microsoft.IdentityModel.Tokens; using System; using System.IdentityModel.Tokens.Jwt; using System.Text; using System.Windows.Forms; public bool ValidateCurrentToken() { string token = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzZXJ2aWNlIjoibWlkZGxld2FyZSIsImlhdCI6MTY2NzIwNDUwM30.xbhwzwHZzNmU623Yp3RTEbramazamhaber3qmYpeuiVEgSFWzlJA97TpnMZ_Wd0xFVVSIjHyZU58C9k-zTkT1r5A"; var mySecurityKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes("ifsramazanfvtPnlv")); var tokenHandler = new JwtSecurityTokenHandler(); try { tokenHandler.ValidateToken(token, new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = mySecurityKey, ValidateLifetime = false, ValidateIssuer = false, ValidateAudience = false, }, out SecurityToken validatedToken); } catch (SecurityTokenSignatureKeyNotFoundException ex) { Console.WriteLine("Signature validation failed " + ex.Message); } catch (SecurityTokenNoExpirationException ex) { Console.WriteLine("Lifetime validation failed " + ex.Message); } catch (Exception ex) { Console.WriteLine("Other exception: " + ex.Message); } return true; } public void GetPayload(string token) { var handler = new JwtSecurityTokenHandler(); var tokenData = handler.ReadJwtToken(token); JwtPayload payload= tokenData.Payload; JwtHeader header= tokenData.Header; foreach (var item in payload) { Console.WriteLine(item.Key+":"+item.Value+"\n"); } } |