title: Parse JWT Token to get the payload content only without external library in C# or Blazor
body: <p>I am writing the client app with Blazor that has access to a JWT. I'd like to know a simple way to read the token payload content without adding additional dependency because I don't need the other information and don't need to validate the token. I think parsing the payload content should be simple enough to just write it in a method.</p>
  
  <pre><code>    JwtTokenContent ReadJwtTokenContent(string token)
      {
          var content = token.Split('.')[1];
  
          // Exception here, it's not a valid base64 string
          var jsonPayload = Encoding.UTF8.GetString(
              Convert.FromBase64String(content));
  
          Console.WriteLine(jsonPayload);
  
          return JsonSerializer.Deserialize&lt;JwtTokenContent&gt;(jsonPayload);
      }
  </code></pre>
  
  <p>How can I decode the payload? It doesn't seem to be just a base64 string.</p>
  
  <blockquote>
    <p>The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.</p>
  </blockquote>
  
  <p>This is an example JWT token:</p>
  
  <blockquote>
    <p>eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJJZCI6IjUyYjg3ZTUwLTViYmMtNDE0Ny1iOTMwLWY2ZGI0ZTUyNDEwYiIsIlVzZXJuYW1lIjoiZGF0dm1Ab3V0bG9vay5jb20iLCJpc3MiOiJodHRwczovL2xvY2FsaG9zdDo0NDMyMi8ifQ.4wKxDCwQ6onvNA_atndSitGjufR-tXutWq-tRNhqKzc</p>
  </blockquote>
  
  <p>(content is <code>eyJJZCI6IjUyYjg3ZTUwLTViYmMtNDE0Ny1iOTMwLWY2ZGI0ZTUyNDEwYiIsIlVzZXJuYW1lIjoiZGF0dm1Ab3V0bG9vay5jb20iLCJpc3MiOiJodHRwczovL2xvY2FsaG9zdDo0NDMyMi8ifQ</code>)</p>
  
  <hr>
  
  <p>Thanks to Kalten, here's the solution for my case:</p>
  
  <pre><code>    JwtTokenContent ReadJwtTokenContent(string token)
      {
          var content = token.Split('.')[1];
          Console.WriteLine(content);
  
          var jsonPayload = Encoding.UTF8.GetString(
              this.Decode(content));
          Console.WriteLine(jsonPayload);
  
          return JsonSerializer.Deserialize&lt;JwtTokenContent&gt;(jsonPayload);
      }
  
      byte[] Decode(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 2: output += "=="; break; // Two pad chars
              case 3: output += "="; break; // One pad char
              default: throw new System.ArgumentOutOfRangeException("input", "Illegal base64url string!");
          }
          var converted = Convert.FromBase64String(output); // Standard base64 decoder
          return converted;
      }
  </code></pre>