JSON 网络令牌 - 如何识别用户?

JSON web token - how to identify user?

基本上,我使用的是 JWT 身份验证,在成功登录后我将返回 JWT 令牌、到期时间和用户的 GUID(我应该吗?)

让我们举个例子:

我有一个端点 returns 需要验证的用户特定数据

那么,我应该怎么做呢?

假设

user1 生成了他的 jwt:"a.b.c"

user2 生成了他的 jwt:"c.b.a"

如何确保用户 1 不会使用他的 JWT 访问用户 2 的数据?

我考虑过(正如我之前提到的)使用 JWT 令牌发送用户的 GUID 并将其用作识别该用户的方式,但如果某人拥有其他人的 GUID,那么他就能够操纵他的数据,这不好。

提前致谢。

如果 user1 和 user2 通过单独的 ip 发送请求,您可以通过连接用户名和客户端 ip 生成 jwt 令牌,并且在验证令牌时检查从验证请求用户名+ip 和用户名+ip 发送的请求来自解密令牌,否则使用数据过滤机制

它回答了我的问题:

@Mardoxx

How can I ensure that user1 will not use his JWT to access user's 2 data? jwt payload usually has sub claim which is an identifier of who the token was issued to. Store userid in this then check the two. Then jwt token owner can only access its own matching userid data.

if user1 and user2 send request by seprated ip, you can generate jwt token by concating username and client ip and when validating token check that request sent from validate request username+ip and username+ip from decrypted token else using data filter Mechanism

You should be able to retrieve a claims like this within your controller

var identity = HttpContext.User.Identity as ClaimsIdentity;
if (identity != null)
{
    IEnumerable<Claim> claims = identity.Claims; 
    // or
    identity.FindFirst("ClaimName").Value;
}