C# - How to decode JWT Token? | How to decode JWT(Json Web Token) token by using C#


c# Jwt token decoder

In C#, decoding a JWT (JSON Web Token) is a common task when working with authentication and authorization. 
You can use libraries like System.IdentityModel.Tokens.Jwt or third-party libraries like Microsoft.IdentityModel.Tokens.JWT.

-> Input String: (Base64(Header.Payload),private key) here Payload only decoded
string[] Jwt = (TokenText).ToString().Split('.'); // Input
         byte[] Response = Base64UrlDecodeNew(Jwt[1]); // Payload only taken
         FinalJwtPaymentDecode = Encoding.UTF8.GetString(Response);
private static byte[] Base64UrlDecodeNew(string input)
        {
            var output = input;
            output = output.Replace('-', '+'); // 62nd char of encoding
            output = output.Replace('_', '/'); // 63rd char of encoding
            switch (output.Length % 4) // Pad with trailing '='s
            {
                case 0: break; // No pad chars in this case
                case 1: output += "==="; break; // Three pad chars
                case 2: output += "=="; break; // Two pad chars
                case 3: output += "="; break; // One pad char
                default: throw new System.Exception("Illegal base64url string!");
            }
            var converted = Convert.FromBase64String(output); // Standard base64 decoder
            return converted;
        }
Response like: {"REFNo":"N9855346","Transaction":"Accepted"}

Post a Comment

0 Comments