How to decode JWT Token?

匿名 (未验证) 提交于 2019-12-03 01:25:01

问题:

I don't understand how this library works. Could you help me please ?

Here is my simple code :

public void TestJwtSecurityTokenHandler()     {         var stream =             "eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJJU1MiLCJzY29wZSI6Imh0dHBzOi8vbGFyaW0uZG5zY2UuZG91YW5lL2NpZWxzZXJ2aWNlL3dzIiwiYXVkIjoiaHR0cHM6Ly9kb3VhbmUuZmluYW5jZXMuZ291di5mci9vYXV0aDIvdjEiLCJpYXQiOiJcL0RhdGUoMTQ2ODM2MjU5Mzc4NClcLyJ9";         var handler = new JwtSecurityTokenHandler();          var jsonToken = handler.ReadToken(stream);     } 

This is error :

The string needs to be in compact JSON format, which is of the form: Base64UrlEncodedHeader.Base64UrlEndcodedPayload.OPTIONAL,Base64UrlEncodedSignature'.

If you copy stream in jwt.io website, it works fine :)

Thanks in advance for your help !

回答1:

I found solution, I just missed to Cast result as follow :

var tokenS = handler.ReadToken(tokenJwtReponse.access_token) as JwtSecurityToken; 

After I can get Claims like :

 var jti = tokenS.Claims.First(claim => claim.Type == "jti").Value; 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!