ahelland/AADGuide-CodeSamples

Empty Payload / Payload not displaying

Closed this issue · 3 comments

I am getting a blank payload when trying to read. I did have to change the input and output to work from a Console App. Do you know why I might be getting an empty payload.

.WriteLine("Enter JWT");
string jwtInput = Console.ReadLine();
string jwtOutput = "";

        //and the output will be placed in a control called txtJwtOut
        JwtSecurityTokenHandler jwtHandler = new JwtSecurityTokenHandler();
        // string jwtInput = txtJwtIn.Text;

        //Check if readable token (string is in a JWT format)
        bool readableToken = jwtHandler.CanReadToken(jwtInput);

        if (readableToken != true)
        {
            Console.WriteLine("The token doesn't seem to be in a proper JWT format."); ;
        }
        if (readableToken == true)
        {
            JwtSecurityToken token = jwtHandler.ReadJwtToken(jwtInput);

            //Extract the headers of the JWT
            JwtHeader headers = token.Header;
            string jwtHeader = "{";

            foreach (KeyValuePair<string, object> h in headers)
            {
                jwtHeader += '"' + h.Key + "\":\"" + h.Value + "\",";
            }
            jwtHeader += "}";
            Console.WriteLine("Header:\r\n" + JToken.Parse(jwtHeader).ToString(Formatting.Indented));

            //Extract the payload of the JWT
            IEnumerable<Claim> claims = token.Claims;
            string jwtPayload = "{";
            foreach (Claim c in claims)
            {
                jwtPayload += '"' + c.Type + "\":\"" + c.Value + "\",";
            }
            jwtPayload += "}";
            jwtOutput += "\r\nPayload:\r\n" + JToken.Parse(jwtPayload).ToString(Formatting.Indented);
            Console.WriteLine(jwtOutput.ToString());
            Console.ReadLine();
        }

Right off the bat I don't know why that's happening.

I have a slightly different implementation of the parsing here though that you can try: https://gist.github.com/ahelland/f7151b665c09193bae5e1f05766260a4