Home:ALL Converter>Check JWT token in blacklist

Check JWT token in blacklist

Ask Time:2021-01-12T01:23:34         Author:hiep nguyenduc

Json Formatter

I am using web API .net core 3.1, my case is that when a user logs out I will put that user's JWT token in a blacklist that is stored in the database so that the JWT token cannot be accessed into the system even though it has not expired.

Currently, I put the 'CheckJWTTokenInBlacklist' function in each API function as shown below.

        [Authorize]
        [HttpPost]
        public ActionResult Add(OrderRequest req)
        {
            if (ModelState.IsValid)
            {
                string token = HttpContext.Response.Headers["Authorization"]; 
                if (_coffeeToken.CheckJWTTokenInBlacklist(token) == false) //Check whether the token is blacklisted.
                {
                    //token is not in blacklist
                   
                }
                else
                {
                   // token is in blacklist
                }
            }
            return BadRequest(ModelState);
        }

I think this is not the best way since every API needs JWT tokens I have to set the above check function. Are there better ways? Thank you !!

Author:hiep nguyenduc,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/65671657/check-jwt-token-in-blacklist
yy