I have an ASP.NET Core 8 MVC app. It consumes an external API that uses OAuth authorization.
In some action method of my controller, I need to obtain some data from the API. So I first make a request to an url to obtain a token, then make another request to an endpoint of the API including the obtained access token as a bearer token in a header of the request (using httpClient
).
Note: the obtained token is valid for an hour.
This is working ok, but how can I maintain the obtained token between requests to my ASP.NET Core MVC app? Right now, I am just obtaining a new token every time my action method is called.
I am thinking to save the token into the user session, but but is this a correct/safe way to maintain the token between requests to my app?
By the way: as of now I am creating an instance of HttpClient
in the action method, but I am planning on using IHttpClientFactory
with typed-clients... will that make any difference regarding this question?
I have an ASP.NET Core 8 MVC app. It consumes an external API that uses OAuth authorization.
In some action method of my controller, I need to obtain some data from the API. So I first make a request to an url to obtain a token, then make another request to an endpoint of the API including the obtained access token as a bearer token in a header of the request (using httpClient
).
Note: the obtained token is valid for an hour.
This is working ok, but how can I maintain the obtained token between requests to my ASP.NET Core MVC app? Right now, I am just obtaining a new token every time my action method is called.
I am thinking to save the token into the user session, but but is this a correct/safe way to maintain the token between requests to my app?
By the way: as of now I am creating an instance of HttpClient
in the action method, but I am planning on using IHttpClientFactory
with typed-clients... will that make any difference regarding this question?
1 Answer
Reset to default 0I would recommend that you use Duende.AccessTokenManagement to cache and acquire new access tokens. You usually want to renew your access tokens on a regular basis.
This blog post Improving ASP.NET Core Security By Putting Your Cookies On A Diet is also a good read about using a SessionStore to store the cookie content.
IHttpClientFactory
with typed client to add token to request. – Zhi Lv Commented yesterday