C# 如何使用System.IdentityModel.Tokens.Jwt包提取令牌过期时间

C# 如何使用System.IdentityModel.Tokens.Jwt包提取令牌过期时间,c#,.net-core,adal,C#,.net Core,Adal,我想在.NET核心项目中使用JSON web令牌进行身份验证。这就是为什么我在其中添加了System.IdentityModel.Tokens.Jwt包 我很熟悉,它提供了验证和解码令牌的功能 大多数情况下,我只需要提取有效负载以获取用户和其他信息,但在某些情况下,我还需要知道令牌何时过期(例如,通过将令牌存储到数据库并在过期后将其删除来使令牌失效) 我从这个示例代码开始 public object ValidateAndDecodeToken(string token) { Symme

我想在.NET核心项目中使用JSON web令牌进行身份验证。这就是为什么我在其中添加了
System.IdentityModel.Tokens.Jwt

我很熟悉,它提供了验证和解码令牌的功能

大多数情况下,我只需要提取有效负载以获取用户和其他信息,但在某些情况下,我还需要知道令牌何时过期(例如,通过将令牌存储到数据库并在过期后将其删除来使令牌失效)

我从这个示例代码开始

public object ValidateAndDecodeToken(string token)
{
    SymmetricSecurityKey symmetricSecurityKey = GenerateSymmetricSecurityKey("db3OIsj+BXE9NZDy0t8W3TcNekrF+2d/1sFnWG4HnV8TZY30iTOdtVWJG8abWvB1GlOgJuQZdcF2Luqm/hccMw=="); // From config
        
    try
    {
        JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();
            
        TokenValidationParameters tokenValidationParameters = new TokenValidationParameters()
        {
            ValidateIssuerSigningKey = true,
            IssuerSigningKey = symmetricSecurityKey
        };
            
        tokenHandler.ValidateToken(token, tokenValidationParameters, out SecurityToken validatedToken);

        DateTime tokenExpiresAt = DateTime.Now; // TODO
        JwtSecurityToken jwtSecurityToken = tokenHandler.ReadJwtToken(encodedToken);
        Dictionary<string, string> tokenPayload = jwtSecurityToken.Claims.ToDictionary(claim => claim.Type, claim => claim.Value);

        return new { token, tokenExpiresAt, tokenPayload };
    }
    catch
    {
        throw;
    }
}

private SymmetricSecurityKey GenerateSymmetricSecurityKey(string base64Secret)
{
    byte[] symmetricKey = Convert.FromBase64String(base64Secret);
    return new SymmetricSecurityKey(symmetricKey);
}
public对象validateAndeCodeToken(字符串标记)
{
SymmetricSecurityKey SymmetricSecurityKey=生成SymmetricSecurityKey(“db3OIsj+BXE9NZDy0t8W3TcNekrF+2d/1SFNWG4HNV8TZY30ITODVWJG8ABWVB1GLogJUQZDCF2LUQM/hccMw=”;//来自配置
尝试
{
JwtSecurityTokenHandler tokenHandler=新的JwtSecurityTokenHandler();
TokenValidationParameters TokenValidationParameters=新的TokenValidationParameters()
{
ValidateSuersigningKey=true,
IssuerSigningKey=symmetricSecurityKey
};
ValidateToken(令牌、令牌验证参数、out SecurityToken ValidateToken);
DateTime-tokenExpiresAt=DateTime.Now;//TODO
JwtSecurityToken JwtSecurityToken=tokenHandler.ReadJwtToken(encodedToken);
Dictionary tokenPayload=jwtSecurityToken.Claims.ToDictionary(claim=>claim.Type,claim=>claim.Value);
返回新的{token,tokenExpiresAt,tokenPayload};
}
抓住
{
投掷;
}
}
私有SymmetricSecurityKey生成SymmetricSecurityKey(基于字符串的64密钥)
{
字节[]symmetricKey=Convert.FromBase64String(base64Secret);
返回新的SymmetricSecurityKey(symmetricKey);
}

正如您在这里看到的,我试图提取令牌过期时间和有效负载。我认为有效负载应该可以正常工作,但如何提取到期信息?

从示例代码中,您应该能够获得到期时间,如下所示:

tokenHandler.ValidateToken(令牌、令牌验证参数、out SecurityToken ValidateToken);
var tokenExpiresAt=validatedToken.ValidTo;

谢谢您的回复。我几乎知道了,但我缺少的是令牌过期时间:)我更新了我的问题,以显示实际代码和丢失的内容根据新的示例更新了答案:-)针对您描述的用例(例如,通过将令牌存储到数据库中并在过期后将其删除来使其无效)您最好适当地设置
tokenValidationParameters
(即验证到期),然后删除无效的令牌,而不是直接使用到期时间。谢谢您的帮助!你介意再解释一下你的第二条评论吗?或者提供一个小的代码示例?@ChrisM我也想知道:)因为我看到OP中的代码会抛出异常,因为观众没有被验证。但我在问自己,为什么要验证访问群体url或令牌过期?