Forum Discussion

josh-monreal's avatar
josh-monreal
Copper Contributor
Nov 10, 2021
Solved

Access token exhaustion

Hi, I am working on a .NET REST API project which can be accessed via the client credentials flow. Every time a client application invokes our API, it has to request for an access token first.    M...
  • neptr's avatar
    neptr
    Nov 12, 2021

    josh-monreal 

     

    Basically, the point of my question is this: If I am the developer of the client application, what is the best practice in ensuring that it uses an access token until it expires and only request a new one once that happens? - Josef Ottosson describes a good starting point for that in https://josef.codes/dealing-with-access-tokens-in-dotnet/.

     

    If the client application was an ASP.NET application, then I would build a service through which all requests are made to the api. This service would hold the access token across all requests. Before each request, this service would check if the access token is still valid.

     

    The service could check the validity of the access token based on the liftetime and the time when this token was received. If the token expired, the service would request a new access token and hold it again.

     

    Holding the access token across multiple requests can be achieved by adding the service as services.AddSingleton<ICompanyRestService, CompanyRestService>(); Even better would be to add a separate service TokenStoreService or something like that, which is then used by the CompanyRestService.

     

    It would also be possible to make requests to the api until it eventually returns 401 or so and then update the token and repeat the request.

     

    https://github.com/joseftw/jos.tokens/blob/develop/src/JOS.Tokens/HttpClients/CompanyHttpClient5.cs

     

Resources