【发布时间】:2020-06-23 15:38:26
【问题描述】:
我正在加密这样的令牌:
var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(_configuration.GetSection("TokenAuthentication:SecretKey").Value));
var encryptingCredentials = new EncryptingCredentials(signingKey, JwtConstants.DirectKeyUseAlg, SecurityAlgorithms.Aes256CbcHmacSha512);
// Create the JWT and write it to a string
var jwtSecurityToken = new JwtSecurityTokenHandler().CreateJwtSecurityToken(
issuer: _configuration.GetSection("TokenAuthentication:Issuer").Value,
audience: _configuration.GetSection("TokenAuthentication:Audience").Value,
subject: new ClaimsIdentity(claims),
notBefore: now,
expires: now.Add(TimeSpan.FromMinutes(5)),
now,
signingCredentials: new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256),
encryptingCredentials: encryptingCredentials
);
var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken);
在 dotnet 核心服务中,我希望能够在 JavaScript 中解密令牌。我已经尝试了来自“Decrypting signature and Veryifying JWT”的解决方案,但无法比“错误:格式错误的 UTF-8 数据”更进一步。
有简单的解决办法吗?
【问题讨论】:
-
有什么理由像链接的 q/a 中那样手动进行吗?通常我会建议使用众多JWT libraries 之一。顺便提一句。要获取 JWT 的有效负载数据,您只需 decode JWT,因为它只是 base64url encoded,not encrypted
-
或许你可以试试这个功能stackoverflow.com/questions/38552003/…
-
为什么要“解密”JWT?你期望从中得到什么,为什么? JWT 应该被视为传递给服务器的令牌。除了通过 HTTP 请求传递它们之外,客户端与它们没有任何关系?
-
@Liam 这是真的。尤其是。当您使用对称密钥时(如问题中所示),在客户端共享密钥是一个很大的禁忌!
-
好的,所以可能会丢失加密?我想解码密钥以获取声明、用户名等。
标签: javascript .net-core jwt