title: JWT token exception in ASP.Net (Lifetime validation failed. The token is missing an Expiration Time.)
body: <p>I am creating my own custom authentication on ASP. Net MobileService deployed on Azure. I use JWT tokens. Here is how I generate a new token (claimType = email):</p>
  <pre><code>    public static string GetSecurityToken(String email)
      {
          var symmetricKey = Convert.FromBase64String(signingKey);
          var tokenHandler = new JwtSecurityTokenHandler();
  
          var now = DateTime.UtcNow;
          var tokenDescriptor = new SecurityTokenDescriptor
          {
              Subject = new ClaimsIdentity(new[]
                      {
                      new Claim(ClaimTypes.Email, email)
                  }),
              NotBefore = now,
              Expires = now.AddYears(10),
              Issuer = issuer,
              Audience = audience,
              IssuedAt = now,
              SigningCredentials = new SigningCredentials(
                  new SymmetricSecurityKey(symmetricKey),
                  SecurityAlgorithms.HmacSha256Signature),
          };
  
          var stoken = tokenHandler.CreateToken(tokenDescriptor);
          var token = tokenHandler.WriteToken(stoken);
  
          return token;
      }
  </code></pre>
  <p>The token is sent to the client and stored. But when I try to authorize a message based on its token, I get the error:</p>
  <blockquote>
  <p>Lifetime validation failed. The token is missing an Expiration Time.</p>
  </blockquote>
  <p>This is how I try to validate the token:</p>
  <pre><code>    public static ClaimsPrincipal GetPrincipal(string token)
      {
          try
          {
              var tokenHandler = new JwtSecurityTokenHandler();                
              var jwtToken = tokenHandler.ReadToken(token) as JwtSecurityToken;
              
              if (jwtToken == null)
                  return null;
  
              var symmetricKey = Convert.FromBase64String(signingKey);
  
              Debug.WriteLine(String.Format(&quot;JWTManager &gt; GetPrincipal &gt; Validating Token: {0}&quot;, token));
              foreach (Claim claim in jwtToken.Claims)
              {
                  Debug.WriteLine(String.Format(&quot;JWTManager &gt; GetPrincipal &gt; Claims: {0}&quot;, claim.ToString()));
              }
              
              var validationParameters = new TokenValidationParameters()
              {
                  //RequireExpirationTime = true,
                  //ValidateLifetime = true,
                  ValidateIssuer = true,
                  ValidateAudience = true,
                  IssuerSigningKey = new SymmetricSecurityKey(symmetricKey),
              };
              
              SecurityToken securityToken;
              var principal = tokenHandler.ValidateToken(token, validationParameters, out securityToken);
              if (principal != null)
                  Debug.WriteLine(String.Format(&quot;JWTManager &gt; GetPrincipal &gt; Principal: {0}&quot;, principal));
              return principal;
          }
          catch (SecurityTokenException ex)
          {
              Debug.WriteLine(String.Format(&quot;JWTManager &gt; GetPrincipal: {0}&quot;, ex.Message));
              return null;
          }
          catch (Exception ex)
          {
              Debug.WriteLine(String.Format(&quot;JWTManager &gt; GetPrincipal: {0}&quot;, ex.Message));
              return null;
          }
      }
  </code></pre>
  <p>Exception is thrown on executing <code>tokenHandler.ValidateToken</code> and null is returned to <code>principal</code>.</p>
  <p>My assumption is that maybe I am not setting the <code>Expires</code> and <code>Issuers</code> properties correctly and the TokenHanlder fails to validate them. However, when I check the jwtToken, all the claims are correctly set.</p>
  <p>Here is the complete debug output:</p>
  <blockquote>
  <p>JWTManager &gt; GetPrincipal &gt; Claims: email: testEmail@email.com</p>
  <p>JWTManager &gt; GetPrincipal &gt; Claims: nbf: 1494752301</p>
  <p>JWTManager &gt; GetPrincipal &gt; Claims: exp: 33051661101</p>
  <p>JWTManager &gt; GetPrincipal &gt; Claims: iat: 1494752301</p>
  <p>JWTManager &gt; GetPrincipal &gt; Claims: iss: MASKED</p>
  <p>JWTManager &gt; GetPrincipal &gt; Claims: aud: MAKSED</p>
  <p>JWTManager &gt; GetPrincipal: IDX10225: Lifetime validation failed. The token is  missing an Expiration Time. Application: Tokentype:</p>
  </blockquote>