1

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.

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...
    }
}
Sandeep Rasgotra
  • 582
  • 1
  • 7
  • 18

1 Answers1

3

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

Try code below:

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;
}
Edward
  • 28,296
  • 11
  • 76
  • 121