Skip to content

Instantly share code, notes, and snippets.

@xximjasonxx
Created March 10, 2021 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xximjasonxx/c41e946be11bf039402538e345b404b2 to your computer and use it in GitHub Desktop.
Save xximjasonxx/c41e946be11bf039402538e345b404b2 to your computer and use it in GitHub Desktop.
public class ProcessApiResultFilter : IActionFilter
{
private readonly JwtTokenService _jwtTokenService;
public ProcessApiResultFilter(JwtTokenService jwtTokenService)
{
_jwtTokenService = jwtTokenService;
}
public void OnActionExecuting(ActionExecutingContext context)
{
// no action
}
public void OnActionExecuted(ActionExecutedContext context)
{
if ((context.Result as OkObjectResult)?.Value is ApiResult result)
{
if (result.TokensChanged)
{
var newTokenString = _jwtTokenService.CreateJwtTokenString(
result.NewAccessToken, result.NewRefreshToken).Result;
context.HttpContext.Response.Headers.Add("X-NewToken", newTokenString);
}
context.Result = new ObjectResult(result.Result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment