Jes Sie

Jes Sie

  • 741
  • 1.2k
  • 281.8k

JWT is not well formed

Jun 3 2021 8:15 AM
Again my first time developing an asp.net core rest API. See below snippet:
 
 
 
And below is the error:
 
  1. "errorMessage": "System.ArgumentException: IDX12709: CanReadToken() returned false. JWT is not well formed: 'System.String'.\nThe token needs to be in JWS or JWE Compact Serialization Format. (JWS): 'EncodedHeader.EndcodedPayload.EncodedSignature'. (JWE): 'EncodedProtectedHeader.EncodedEncryptedKey.EncodedInitializationVector.EncodedCiphertext.EncodedAuthenticationTag'.\r\n   at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ReadJwtToken(String token)\r\n   at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateSignature(String token, TokenValidationParameters validationParameters)\r\n   at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.  
Below also is my entire TokenManager:
 
  1. public class TokenManager : ITokenManager  
  2.     {  
  3.         //private List<Token> listTokens;  
  4.         private JwtSecurityTokenHandler tokenHandler;  
  5.         private byte[] secretKey;  
  6.         public TokenManager()  
  7.         {  
  8.             tokenHandler = new JwtSecurityTokenHandler();  
  9.             secretKey = Encoding.ASCII.GetBytes("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");  
  10.             //listTokens = new List<Token>();  
  11.         }  
  12.         public bool Authenticate(string userName, string password)  
  13.         {  
  14.             if (!string.IsNullOrEmpty(userName) &&   
  15.                 !string.IsNullOrEmpty(password) &&  
  16.                 userName.ToLower() == "ltcadmin" &&  
  17.                 password == "ltcpassword")  
  18.                 return true;  
  19.             else  
  20.                 return false;  
  21.         }  
  22.   
  23.         public string NewToken()  
  24.         {  
  25.             var tokenDescriptor = new SecurityTokenDescriptor  
  26.             {  
  27.                 Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, "LTC LMM") }),  
  28.                 Expires = DateTime.UtcNow.AddMinutes(5),  
  29.                 SigningCredentials = new SigningCredentials(  
  30.                     new SymmetricSecurityKey(secretKey),  
  31.                     SecurityAlgorithms.HmacSha256Signature)  
  32.             };  
  33.   
  34.             var token = tokenHandler.CreateToken(tokenDescriptor);  
  35.             var jwtString = tokenHandler.WriteToken(token);  
  36.             return jwtString;  
  37.   
  38.         }  
  39.   
  40.         public ClaimsPrincipal VerifyToken(string token)  
  41.         {  
  42.             var claims = tokenHandler.ValidateToken(token,  
  43.                 new TokenValidationParameters  
  44.                 {  
  45.                     ValidateIssuerSigningKey = true,  
  46.                     IssuerSigningKey = new SymmetricSecurityKey(secretKey),  
  47.                     ValidateLifetime = true,  
  48.                     ValidateAudience = false,  
  49.                     ValidateIssuer = false,  
  50.                     ClockSkew = TimeSpan.Zero  
  51.                 }, out SecurityToken validatedToken);  
  52.             return claims;  
  53.         }  
 
 
It says that the error is in line 53. Thank you in advance. 
 
 

Answers (1)