本文介绍了如何在OnAuthorization()方法上获取令牌详细信息,例如访问令牌,访问令牌到期时间和刷新令牌等.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弹出一个逻辑,如果访问令牌过期,则在 ASP.NET WEB API核心中的onAuthorization(AuthorizationFilterContext上下文)方法上生成刷新令牌.

I want to eject a logic that if access token is expired then generate refresh token on onAuthorization(AuthorizationFilterContext context) method in ASP.NET WEB API Core.

但是我无法找到一种方法来获取令牌详细信息.基本上如何获取令牌详细信息,例如到期时间,从 AuthorizationFilterContext 刷新令牌.

But i am not able to find a way to get token details.Basically how to get token details like expiry , refresh token from AuthorizationFilterContext.

public void OnAuthorization(AuthorizationFilterContext context)
{
    var user = context.HttpContext.User;
    if (!user.Identity.IsAuthenticated)
    {
        var test = context.;
        ..code to get refresh token...
    }
}

推荐答案

对于令牌身份验证,您可以从标头中检索令牌,然后对令牌进行解码.

For token authentication, you could retrive the token from header and then decode the token.

请尝试以下代码:

public void OnAuthorization(AuthorizationFilterContext context)
{
    //var token = context.HttpContext.GetTokenAsync("access_token").GetAwaiter().GetResult();
    var token = context.HttpContext.Request.Headers["Authorization"].FirstOrDefault().Split(" ")[1];
    var handler = new JwtSecurityTokenHandler();
    var jsonToken = handler.ReadToken(token);
    var tokenS = handler.ReadToken(token) as JwtSecurityToken;
}

这篇关于如何在OnAuthorization()方法上获取令牌详细信息,例如访问令牌,访问令牌到期时间和刷新令牌等.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 05:32