首页 新闻 会员 周边

JWT过期的问题

0
悬赏园豆:5 [待解决问题]

如下代码,为什么我设置了过期时间为1秒,为啥几秒后我还是能取到数据
public static string CreateToken(IList<Claim> claims)
{
var key = new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(SecurityKey));
var expires = DateTime.Now.AddSeconds(1);
var token = new JwtSecurityToken(
issuer: issuer,
audience: audience,
claims: claims,
notBefore: DateTime.Now,
expires: expires,
signingCredentials: new SigningCredentials(key, SecurityAlgorithms.HmacSha256Signature)); ;
//生成Token
string jwtToken = new JwtSecurityTokenHandler().WriteToken(token);
return jwtToken;
}

    public static void RefreshToken(string token)
    {
      JwtSecurityToken jwtSecurityToken= new JwtSecurityTokenHandler().ReadJwtToken(token);
        IEnumerable<Claim> claims = jwtSecurityToken.Claims;
    }
灬丶的主页 灬丶 | 初学一级 | 园豆:2
提问于:2019-10-23 10:02
< >
分享
所有回答(1)
0

就是能取到的,这个设计就是jwt解密后 拿到的时间由自己服务端来做过期判断,要是过期了就不能jwt解密的话,为什么不使用cookie或者session

whyong | 园豆:202 (菜鸟二级) | 2019-10-24 08:56

我已经知道原因了,.net已经封装了对jwt的过期判断的,我不用cookie的原因是觉得前后端分离的话cookie不好弄

支持(0) 反对(0) 灬丶 | 园豆:2 (初学一级) | 2019-10-24 09:43

@灬丶: 原来如此 我也学习到了 谢谢

支持(0) 反对(0) whyong | 园豆:202 (菜鸟二级) | 2019-10-24 09:50

@whyong:配置jwt时注册一个事件,过期了就会进入到这个方法里面
jwtBearerOptions.Events = new JwtBearerEvents
{
OnAuthenticationFailed = context =>
{
if(context.Exception.GetType()==typeof(SecurityTokenExpiredException))
{
context.Response.Headers.Add("isExpires", "true");
}
return Task.CompletedTask;
}
};

支持(0) 反对(0) 灬丶 | 园豆:2 (初学一级) | 2019-10-24 10:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册