Handle 401 response with JWT authentication in generated C# client #4857
Unanswered
JonWillis-Tote
asked this question in
Q&A
Replies: 1 comment
-
|
Just as an update, to work around this, I wrapped the generated client in my own client, and did all the additional logic required in that. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I have an OpenAPI spec (snipped to just the security bits below), which I can use in the NSwagStudio to generate the c# client. We need to call an endpoint with a JWT token, and if we get back 401 response, we should reauthenticate to get a new JWT. From what I can tell, the client generation does nothing with the JWT authentication in the schema?
I know I can inject a HttpClient to the generated constructor, so I can authenticate in advance and set the default headers. I can also also if needed, implement the PrepareRequest method to get a JWT token on every request.
My issue is specifically around reauthentication based on a response. My JWT is valid for 10 minutes, so i don't want to request a new JWT on every request. Instead, I expect if I get a 401 response, then I want to trigger authentication and reattempt the request with the new JWT. I believe this is a normal flow for JWT auth as tokens have reasonable short expiry times.
Now I can't see anything within the generated clients that handle this case, or have I missed something?
Beta Was this translation helpful? Give feedback.
All reactions