title: c# How to verify signature JWT?
body: <p>I have a token, a file containing public key and I want to verify the signature.
  I tried to verify signature based on <a href="https://stackoverflow.com/questions/10055158/is-there-a-json-web-token-jwt-example-in-c">this</a>. </p>
  
  <p>However, decodedCrypto and decodedSignature don't match.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public static string Decode(string token, string key, bool verify)
      {
          var parts = token.Split('.');
          var header = parts[0];
          var payload = parts[1];
          byte[] crypto = Base64UrlDecode(parts[2]);
  
          var headerJson = Encoding.UTF8.GetString(Base64UrlDecode(header));
          var headerData = JObject.Parse(headerJson);
          var payloadJson = Encoding.UTF8.GetString(Base64UrlDecode(payload));
          var payloadData = JObject.Parse(payloadJson);
  
          if (verify)
          {
              var bytesToSign = Encoding.UTF8.GetBytes(string.Concat(header, ".", payload));
              var keyBytes = Encoding.UTF8.GetBytes(key);
              var algorithm = (string)headerData["alg"];
              var signature = HashAlgorithms[GetHashAlgorithm(algorithm)](keyBytes, bytesToSign);
              var decodedCrypto = Convert.ToBase64String(crypto);
              var decodedSignature = Convert.ToBase64String(signature);
  
              if (decodedCrypto != decodedSignature)
              {
                  throw new ApplicationException(string.Format("Invalid signature. Expected {0} got {1}", decodedCrypto, decodedSignature));
              }
          }
  
          return payloadData.ToString();
      }
  </code></pre>
  
  <p>I'm sure that the signature of token is valid. I try to verify on <a href="https://jwt.io/" rel="noreferrer">https://jwt.io/</a> and it showed that Signature verified.
  So the problem is the algorithm to encode, decode.</p>
  
  <p>Is there anyone can solve this problem? The algorithm is RS256</p>
  
  <p><a href="https://i.stack.imgur.com/5M2Dt.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/5M2Dt.jpg" alt="enter image description here"></a></p>