Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force expire a JWT token in ASP.Net Core?

I have implemented a JWT authentication and a policy-based authorization in ASP.NET Core. There is a certain user with admin privileges who can assign permissions to non-admin users. If the admin updates the permissions/claims of a non-admin user, is there a way to force expire the access token so that user carrying it will be forced to request a new access token with the newly updated permissions/claims? Right now, the only way to that is to wait for the token to expire but I want to force expire it immediately.

like image 440
Wendell Avatar asked Nov 27 '25 23:11

Wendell


1 Answers

Authentication based on JWT tokens is stateless in serverside. So when a token is not expired it will work. There are some approaches to the problem:

  • Not including the roles and permissions in the token claims and getting these values from the database in each request.
  • Using refresh token mechanism and set a refreshing time to a few minutes and return a new token when refreshing time is expired. Therefore for the tokens with the expired refreshing time you know to get the new access permissions(not in each request). New permissions will set in few minutes but the authenticated user doesn't need to log in again.
  • Creating a set of black-list tokens and append the last issued token to that (not recommended).
like image 75
Hadi Samadzad Avatar answered Nov 30 '25 12:11

Hadi Samadzad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!