{"id":225,"date":"2023-09-05T10:56:02","date_gmt":"2023-09-05T09:56:02","guid":{"rendered":"https:\/\/guillemrueda.com\/blog\/?p=225"},"modified":"2023-09-05T10:56:02","modified_gmt":"2023-09-05T09:56:02","slug":"refresh-token-in-a-nswag-c-client","status":"publish","type":"post","link":"https:\/\/guillemrueda.com\/blog\/2023\/09\/05\/refresh-token-in-a-nswag-c-client\/","title":{"rendered":"Refresh token in a NSwag C# Client"},"content":{"rendered":"\n<p>I use NSwag also to generate the API Clients in VS (using the VS extension REST API Code Generator for VS).<\/p>\n\n\n\n<p>My approach was to override SendAsync using DelegatingHandler. In this handle is possible to catch the response of SendAsync and then manage to refresh the token. I found a kind of implementation in\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/aspnet\/web-api\/overview\/advanced\/httpclient-message-handlers\">the docs<\/a>, but in\u00a0<a href=\"https:\/\/stackoverflow.com\/a\/56204930\/2219577\">this answer<\/a>\u00a0you can find the source of my approach to implement the handle:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/Ruekov\/ad21240a5ceb01d4870e231e3c1e06c5.js\"><\/script>\n\n\n\n<p>I used a global\/static variable to store the refresh tokens and other authentication data required.<\/p>\n\n\n\n<p>In my case I had no chance to use dependency injection (DI) as the answer I linked before. Then, before using the service, I create a HttpClient and I assign the CustomDelegatingHandler to this HttpClient.<\/p>\n\n\n","protected":false},"excerpt":{"rendered":"<p>I use NSwag also to generate the API Clients in VS (using the VS extension REST API Code Generator for VS). My approach was to override SendAsync using DelegatingHandler. In this handle is possible to catch the response of SendAsync and then manage to refresh the token. I found a kind of implementation in\u00a0the docs, &hellip; <a href=\"https:\/\/guillemrueda.com\/blog\/2023\/09\/05\/refresh-token-in-a-nswag-c-client\/\" class=\"more-link\">Continua la lectura de <span class=\"screen-reader-text\">Refresh token in a NSwag C# Client<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,19,6],"tags":[],"class_list":["post-225","post","type-post","status-publish","format-standard","hentry","category-net-programacio","category-c","category-programacio"],"_links":{"self":[{"href":"https:\/\/guillemrueda.com\/blog\/wp-json\/wp\/v2\/posts\/225","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/guillemrueda.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/guillemrueda.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/guillemrueda.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/guillemrueda.com\/blog\/wp-json\/wp\/v2\/comments?post=225"}],"version-history":[{"count":2,"href":"https:\/\/guillemrueda.com\/blog\/wp-json\/wp\/v2\/posts\/225\/revisions"}],"predecessor-version":[{"id":228,"href":"https:\/\/guillemrueda.com\/blog\/wp-json\/wp\/v2\/posts\/225\/revisions\/228"}],"wp:attachment":[{"href":"https:\/\/guillemrueda.com\/blog\/wp-json\/wp\/v2\/media?parent=225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/guillemrueda.com\/blog\/wp-json\/wp\/v2\/categories?post=225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/guillemrueda.com\/blog\/wp-json\/wp\/v2\/tags?post=225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}