Where and how to store the access token and refresh token

Viewed 8284

I have a dotnet core 2.2 MVC web application which uses a web api to perform some database queries. I have implemented JWT token based authetication for web api. Tokens are generated at the api and the web application has received the access token, expiry and refresh token. I need to store this token details at my client , so that I can either use it to access web api(before expiry) or generate new token using the refresh token if the token expires.

Any help on this would be appreciated.

4 Answers

Previous answers don't provide clear explanation about the reasons of using those solutions.

Typical systems look like on the picture below and there are two common Client Application architectures used in WEB:

  • Singe Page Application running in browser
  • Server side MVC application

OAuth system

In case of SPA the tokens are stored in browser (session storage or local storage) and are cleared automatically by either browser or the app itself when expire. FYI, obtaining refresh token is not possible in SPA because of security reasons.

In case of MVC app (your case) the things get more complicated. You have two options: either store it in http-only cookie or some external session store. Aspnet core supports both cases but each has caveats. A good article here. In short, if your are concerned about cookie size then use Distributed Session Storage which adds more complexity to the system architecture. Otherwise cookies is the easiest solution and is enabled by default in aspnet core, you just need to set options.StoreTokens = true and options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme.

There are multiple ways to store the tokens. Usually applications doesn't store access token anywhere, but they do store refresh token in a permanent storage.

Let's take a look at what you need to store at web and api end.

First, user will request to login in web application with credentials, web app will pass this request to the api project - which interacts with DB.

Now, api will generate access tokens and refresh token and the save refresh token to that DB. Web api then need to store access token and refresh token in temporary storage like cookie or session.

When access token is expired; you need to make a call for a new tokens, which will update the previous refresh token in the DB.

TL;DR

Refresh token - in DB

Access token and refresh token - web temporary storage

You have various options (secure http-only cookie, localstorage, session storage, etc.).

In the most simple scenario, you can store it in a cookie so that it is sent along with each request :

  • The cookie should always have the HttpOnly flag to prevent XSS attacks in the browser.
  • The cookie should also use the Secure flag in production, to ensure that the cookie is only sent over HTTPS.
  • Protect your forms against CSRF attacks (by using ASP.NET Core’s AntiForgery features, for example).

Make the call from ui to web application server(controller) controller which in turn makes call to get the token from api. get the token from api response and store it in cookie.

you controller should look something like this

var option = new CookieOptions
{
    Expires = DateTime.Now.AddMinutes(response.ExpiresIn)
};

if (!string.IsNullOrEmpty(domain))
{
                option.Domain = domain;
}

Response.Cookies.Append({cookiename}, response.AccessToken, option);
Related