My Project:
Web API project - ASP .NET Framework 4.8
Problem?
The code flow is as follows:
1.) The API is called -> it must call another API -> 2.) Get JWT authentication token -> 3.) Calls the desired method.
The problem is if my API is called 100 times, I will make 100 calls for the GetJwtToken()
method and another 100 for the desired method itself, which seems like an overhead on the auth server. The token itself has a lifespan of 2 hours.
Are there any documented best practices on how to manage a Web API JWT token in another Web API?
What have I tried?
I've tried the following solutions and I'm still not sure whether they could be considered good practices.
- One static class with two static properties
Token
andValidTo
and one static methodGetJwtToken()
that updates those properties. Before each call to the desired external API method, we check theValidTo
property and update theToken
value if it has expired, via the static method. - In our service, we have one static private field
Token
.The method that calls the external API method is surrounded by atry
catch
blocks. TheCatch(WebException ex)
expects an Unauthorized exception if the token has expired. I check for HTTP Status Code 401 - Unauthorized.
if (response.StatusCode == HttpStatusCode.Unauthorized)
In case we go into that if
clause we update the Token
property by calling the GetJwtToken()
method inside the catch
block and then calling recursively the method again. In this way, we update the token only when it has expired and an unauthorized exception was thrown.
- Another idea that I got, but didn't test is
ActionFilterAttribute
with overriddenOnActionExecuting(HttpActionContext actionContext)
method. Before we go into the Web API controller the action attribute has already checked whether we haveToken
and if it has expired. The problem here was I am not sure where to save theToken
property. Possibly as a static value in another class.
Are there any other ways to manage a JWT Token of a Web API inside another Web API and what is considered best practices?
Some code snippets, pseudo-code, or articles would be appreciated.
Edit1:
I've read this question, but it doesn't help me, since it's about how to manage the token on the front end part. The project here is Web API it's all on the server-side.
Edit2:
Edited some sentences here and there so it's more readable.
Edit3:
Added one more option that I thought about.
I'd handle this in some kind of
BaseApiService
Which would be responsible for making request, response serialization (notice I've used string responses for simplicity sake) and handling token for each request. Also you might want to consider handling errors and also handle infinite loops because it's currently calling self (e.g. on second call if it's unauthorized again, exit with error).
Token handler would be defined as singleton in DI and this is implementation
And this is how you'd consume your
BaseApiService
I don't think you need to add any
ValidTo
logic, but just rely on yourUnauthorized
response from 3rd party API, because you'll just complicate your code and you'll have to handleUnauthorized
responses anyway.Only thing is that you might
lock
getting/setting of token fromTokenHandler
, but this is just a basic example to show an idea how I'd implement it.