Hari Babu

Hari Babu

  • NA
  • 83
  • 0

[Validating JWT token expiry ]

Jan 25 2018 8:36 PM
Hi
 
I am using visual studio framework 4 and implementing JWT token signature validating and checking time expiry.
 
i have implemented until Jwt token signature validation but did not find the solution how to check expiry time to jwt token.
 
Please help me how to check JWT token expiry time. 
 
Code i have implemented
  1. public string verifyJWTToken()  
  2. {  
  3. //string tokenStr = "eyJraWQiOiIxZTlnZGs3IiwiYWxnIjoiUlMyNTYifQ.ewogImlzcyI6ICJodHRwOi8vc2VydmVyLmV4YW1wbGUuY29tIiwKICJzdWIiOiAiMjQ4Mjg5NzYxMDAxIiwKICJhdWQiOiAiczZCaGRSa3F0MyIsCiAibm9uY2UiOiAibi0wUzZfV3pBMk1qIiwKICJleHAiOiAxMzExMjgxOTcwLAogImlhdCI6IDEzMTEyODA5NzAsCiAiY19oYXNoIjogIkxEa3RLZG9RYWszUGswY25YeENsdEEiCn0.XW6uhdrkBgcGx6zVIrCiROpWURs-4goO1sKA4m9jhJIImiGg5muPUcNegx6sSv43c5DSn37sxCRrDZZm4ZPBKKgtYASMcE20SDgvYJdJS0cyuFw7Ijp_7WnIjcrl6B5cmoM6ylCvsLMwkoQAxVublMwH10oAxjzD6NEFsu9nipkszWhsPePf_rM4eMpkmCbTzume-fzZIi5VjdWGGEmzTg32h3jiex-r5WTHbj-u5HL7u_KP3rmbdYNzlzd1xWRYTUs4E8nOTgzAUwvwXkIQhOh5TPcSMBYy6X3E7-_gr9Ue6n4ND7hTFhtjYs3cjNKIA08qm5cpVYFMFMG6PkhzLQ";  
  4. string tokenStr = "eyAidHlwIjogIkpXVCIsICJhbGciOiAiUlMyNTYiIH0.eyAic3ViIjogIlM5ODEyMzgxRCIsICJhdWRpdFRyYWNraW5nSWQiOiAiZjU0Njc3MGQtOTI4Ni00ODNlLTk0YzktZTllNDZlMTMwMzFiIiwgImlzcyI6ICJodHRwOi8vc3RnLWF1dGguYXBwcy5lLm5lY3Rhci5nb3Yuc2c6ODAvY29uc2VudC9vYXV0aDIiLCAidG9rZW5OYW1lIjogImFjY2Vzc190b2tlbiIsICJ0b2tlbl90eXBlIjogIkJlYXJlciIsICJhdXRoR3JhbnRJZCI6ICI4ZDRkNTEwNC0yMmMxLTRkZDYtYTA5Mi0yYmU3YzZlNzkzNDMiLCAiYXVkIjogIm15aW5mbyIsICJuYmYiOiAxNTA1ODkwNzUxLCAic2NvcGUiOiBbICJlZHVsZXZlbCIsICJob3VzaW5ndHlwZSIsICJyYWNlIiwgInJlZ2FkZCIsICJzZXgiLCAiaGRidHlwZSIsICJtb2JpbGVubyIsICJjcGZiYWxhbmNlcyIsICJoYW55dXBpbnlpbm5hbWUiLCAibWFycmllZG5hbWUiLCAiaGFueXVwaW55aW5hbGlhc25hbWUiLCAiYXNzZXNzYWJsZWluY29tZSIsICJtYXJpdGFsIiwgImFsaWFzbmFtZSIsICJuYXRpb25hbGl0eSIsICJkb2IiLCAibmFtZSIsICJjcGZjb250cmlidXRpb25zIiwgImVtYWlsIiBdLCAicmVhbG0iOiAiL215aW5mbyIsICJleHAiOiAxNTA1ODk0MzUxLCAiaWF0IjogMTUwNTg5MDc1MSwgImV4cGlyZXNfaW4iOiAzNjAwMDAwLCAianRpIjogIjI2MmM3ODk5LWY4YmQtNDVlOC05Mzc1LWRmYTY5YWUwNTA3YSIgfQ.maD5lnrRA84hm2Lz5gdUlM6ZVSsYxYlWodtMTesleDXJrD3hDeUVbmHGumeH4XogtmYacCWKh3geY5iItK4WLr_ft5oFzvXilWrud7zahFOR5QuOxhALzl8d1erDN4jCQfbT6xq98B7nRCG9JAB1qvEvnSGQLGSWxH3j4IxE5Jp5iP-3AzshXub-8JukAqnWZLiQsoJCUu8GBg0RfG85wRKwImPmnPyCoAwqTv2MlqhZQr5oXkkeRrqITkZQME3sFiqZmnExjHcml8Y__5h3FNvJSFdnnZSDW0471wBpYM9-jK7WdBR9rKS68sxdNa85y-TQ7YXcb5AHYPro9GGY6w0";  
  5. string[] tokenParts = tokenStr.Split('.');  
  6. X509Certificate2 certificate = new X509Certificate2(Path.Combine(@"C:\Users\haribabu.kommalapati\Downloads\myinfo-demo-app-v1.0\myinfo-demo-app-v1.0\ssl""stg-auth-signing-public.pem"));  
  7. RSACryptoServiceProvider key = certificate.PublicKey.Key as RSACryptoServiceProvider;  
  8. RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();  
  9. if (key != null)  
  10. {  
  11. RSAParameters parameters = key.ExportParameters(false);  
  12. byte[] expoenet = parameters.Exponent;  
  13. byte[] modulus = parameters.Modulus;  
  14. rsa.ImportParameters(  
  15. new RSAParameters()  
  16. {  
  17. Modulus = modulus,  
  18. //FromBase64Url("w7Zdfmece8iaB0kiTY8pCtiBtzbptJmP28nSWwtdjRu0f2GFpajvWE4VhfJAjEsOcwYzay7XGN0b-X84BfC8hmCTOj2b2eHT7NsZegFPKRUQzJ9wW8ipn_aDJWMGDuB1XyqT1E7DYqjUCEOD1b4FLpy_xPn6oV_TYOfQ9fZdbE5HGxJUzekuGcOKqOQ8M7wfYHhHHLxGpQVgL0apWuP2gDDOdTtpuld4D2LK1MZK99s9gaSjRHE8JDb1Z4IGhEcEyzkxswVdPndUWzfvWBBWXWxtSUvQGBRkuy1BHOa4sP6FKjWEeeF7gm7UMs2Nm2QUgNZw6xvEDGaLk4KASdIxRQ"),  
  19. Exponent = expoenet  
  20. //FromBase64Url("AQAB")  
  21. });  
  22. }  
  23. SHA256 sha256 = SHA256.Create();  
  24. byte[] hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(tokenParts[0] + '.' + tokenParts[1]));  
  25. RSAPKCS1SignatureDeformatter rsaDeformatter = new RSAPKCS1SignatureDeformatter(rsa);  
  26. rsaDeformatter.SetHashAlgorithm("SHA256");  
  27. if (rsaDeformatter.VerifySignature(hash, FromBase64Url(tokenParts[2])))  
  28. {  
  29. return Encoding.UTF8.GetString(Base64UrlDecode(tokenParts[1]));  
  30. }  
  31. else  
  32. {  
  33. }  
  34. return string.Empty;  
  35. }  

Answers (1)