我有一个 JWT 令牌,我正在尝试解码有效负载。我要解码的字符串是:eyJzdWIiOiIyMzEzOTE6MTAxMjpFQiBBZG1pbixFQiBDaGVjayBJbi9PdXQgUmVzb3VyY2VzLEVCIE1hc3RlciBBZG1pbiIsImV4cCI6IjE1NDUyNTc2MDkiLCJhdWQiOiI0OTk0QzgxMC02QUMyLTQ3NkYtQjUyQi04MjQ5NUUzRUNBNTgifQ如果我去https://www.base64decode.org/并插入该字符串,它可以解码为:{"sub":"231391:1012:EB Admin,EB Check In/Out Resources,EB Master Admin","exp":"1545257466","aud":"4994C810-6AC2-476F-B52B-82495E3ECA58"}但是有了这个:byte[] dataPayload = Convert.FromBase64String(tokenPayload);我得到:Base-64 字符数组或字符串的长度无效不知道我做错了什么。编辑:这是整个 JWT,由 JWT.IO 提供(我添加了换行符,以便于阅读)...eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9。eyJzdWIiOiIyMzEzOTE6MTAxMjpFQiBBZG1pbixFQiBDaGVjayBJbi9PdXQgUmVzb3VyY2VzLEVCIE1hc3RlciBBZG1pbiIsImV4cCI6IjE1NDUyNTc4ODMiLCJhdWQiOiI0OTk0QzgxMC02QUMyLTQ3NkYtQjUyQi04MjQ5NUUzRUNBNTgifQ. 0oaXmDOlYdHSTLo7t7fFpHG2T0DMRpAoERxiM_Ur5O4同样的两行代码运行良好:string tokenPayload = _jwtData.WebToken.Split('.')[1];byte[] dataPayload = Convert.FromBase64String(tokenPayload);但是由于某种我不明白的原因,现在我收到了这个错误。
2 回答
郎朗坤
TA贡献1921条经验 获得超9个赞
您缺少填充。看看以下内容:https ://en.wikipedia.org/wiki/Base64#Output_padding
So in your case, the correct JWT token is: eyJzdWIiOiIyMzEzOTE6MTAxMjpFQiBBZG1pbixFQiBDaGVjayBJbi9PdXQgUmVzb3VyY2VzLEVCIE1hc3RlciBBZG1pbiIsImV4cCI6IjE1NDUyNTc2MDkiLCJhdWQiOiI0OTk0QzgxMC02QUMyLTQ3NkYtQjUyQi04MjQ5NUUzRUNBNTgifQ==
哔哔one
TA贡献1854条经验 获得超8个赞
似乎 JWT.IO 有自己的方法来解码自己的有效负载。这是正确的方法:
string jwt = JWT.Decode("...token...", "...encoded Secret Key...", JwsAlgorithm.HS256);
JWTJose.JWT
来自他们的 NuGet 包。
- 2 回答
- 0 关注
- 288 浏览
添加回答
举报
0/150
提交
取消