I'm working on a .Net Core project with hundreds of APIs in it.
The APIs accept JWT token to authorize.
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
As JWT tokens cannot be destroyed until the expiration time is over, I need to make sure that the token gets unsuable when the user logsout.
I stored the JWT token with key in the memory cache at it's creation.
memoryCache.Set(ob.MailId, Token.Token);
I want to allow the code execution only if the token exists in the memory cache, so that, when the user logsout, I'll remove the token from the memory cache, then the API will be inaccessible with the same token.
I don't want to put condition check in every APIs and I do not have much detail knowledge with Authorization either.
Is there any way to put a condition along with the header such a way that if the condition fails, it hits the UnauthorizedAccessException?
Please suggest me an efficient way to execute this.
We use http 401 statue code instead of UnauthorizedAccessException to indicate user is unauthenticated in usual.
You could decide where to get the token when you configure JwtAuthentication,a minimal example:
in Program.cs:
AuthController:
Result:
Here's a document related with ASP.NET Core authentication,Hopes help