title: JWT Unable to decode the header as Base64Url encoded string
body: <p>I have the following code:</p>
  
  <pre><code>public async Task&lt;LoginResult&gt; GenerateJwtTokenAsync(string email, string password)
  {
      LoginResult loginResult = await _membershipProvider.Login(email, password);
      if (loginResult.Succeeded)
      {
          var symmetricKey = Convert.FromBase64String(Secret);
  
          var tokenDescriptor = new SecurityTokenDescriptor
          {
              Subject = new ClaimsIdentity(loginResult.Claims),
              Expires = DateTime.UtcNow.AddDays(1),
              SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(symmetricKey), SecurityAlgorithms.HmacSha256Signature)
          };
  
          var stoken = _tokenHandler.CreateToken(tokenDescriptor);
          var token = _tokenHandler.WriteToken(stoken);
  
          // Check token here to see if it works
          var jwtToken = _tokenHandler.ReadToken(token) as JwtSecurityToken;
          loginResult.JwtToken = token;
      }
      return loginResult;
  }
  
  public ClaimsPrincipal ValidateJwtToken(string tokenString)
  {
  
      ClaimsPrincipal principal;
  
      try
      {
          var jwtToken = _tokenHandler.ReadToken(tokenString) as JwtSecurityToken;
  
          if (jwtToken == null)
          {
              principal = null;
          }
          else
          {
              var symmetricKey = Convert.FromBase64String(Secret);
  
              var validationParameters = new TokenValidationParameters()
              {
                  RequireExpirationTime = true,
                  ValidateIssuer = false,
                  ValidateAudience = false,
                  IssuerSigningKey = new SymmetricSecurityKey(symmetricKey)
              };
  
              SecurityToken securityToken;
              principal = _tokenHandler.ValidateToken(tokenString, validationParameters, out securityToken);
          }
      }
      catch (Exception ex)
      {
          principal = null;
      }
  
      return principal;
  }
  </code></pre>
  
  <p>The line below reads the token perfectly, however when I actually read it in the 2nd method I get an exception.</p>
  
  <pre><code>// Check token here to see if it works
  var jwtToken = _tokenHandler.ReadToken(token) as JwtSecurityToken
  </code></pre>
  
  <p>I have verified the two string are identical, I am extremely confused as to why this stops working when I actually want to validate the token for the life of me I can't see what I am doing wrong. Any ideas please?</p>
  
  <p>EDIT:</p>
  
  <p>Exception</p>
  
  <pre><code>   "IDX10729: Unable to decode the header 'header' as Base64Url encoded string. jwtEncodedString: 'Token here'."
  </code></pre>
  
  <p>Stack trace:</p>
  
  <pre><code>   at System.IdentityModel.Tokens.Jwt.JwtSecurityToken.Decode(String[] tokenParts, String rawData)
     at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ReadJwtToken(String token)
     at AuthService.ValidateJwtToken(String tokenString) in AuthService.cs:line 57
  </code></pre>