title: How to use JWT JSON Web Tokens in C# AspNetCore Websites?
body: <p>I am trying to pull together a website that uses JWTs for login.  My problem is that I do not understand how a website should use the JWTs.</p>
  
  <p>The idea is to split my monolithic architecture into:</p>
  
  <ul>
  <li>An IdentityServer for authentication and issuing JWTs.</li>
  <li>An ApplicationServer API that has endpoints protected by JWT based roles.</li>
  <li>Front-end Apps and Websites that request JWTs from the IdentityServer, and use them to access data on the ApplicationServer.</li>
  </ul>
  
  <p>This will allow me to use one IdentityServer for many projects, and possibly farm front end development out to 3rd parties, and allow me to concentrate on the application API details.</p>
  
  <p>I have the IdentityServer built.  It can take a username/password, validate, and issue an access token and a refresh token.  Super.
  The access token is short-lived and non-secure, and intended to be refreshed periodically.  The intention is to store this on the client side, either app or web page.
  The refresh token is longer-lived, and intended to be stored securely.  On a website, this would be stored server-side, and in an app, stored in secure storage for the 'session' duration.</p>
  
  <p>I have a front end website shell set up with some test actions.  The user must be authenticated to access them, where being authenticated means having a valid access token.</p>
  
  <ul>
  <li>When the user accesses an action initially, they have no token.</li>
  <li>The front-end webserver should redirect them to a login page, and request credentials (username and password).</li>
  <li>The webserver forwards credentials to IdentityServer, which should return access and refresh tokens to the webserver if the credentials are valid.</li>
  <li>The webserver would cache the refresh token and send the access token back to the webpage.</li>
  <li>The client-side webpage would then cache the access token and send that with each further page request.</li>
  <li>The webserver would decode the access token, possibly refresh it if necessary, and send back the pages with the updated access token.</li>
  <li>The conversation (session) ends when a request comes in with a stale access token and enough time has elapsed for the refresh token to expire, and the user is re-directed to the login page.</li>
  </ul>
  
  <p>I am a little lost as to how to return the access token to the webpage.</p>
  
  <p>Also, I do not understand how the web page could either automatically attach a token with each request if it has one, or how a web page could respond to a 401 challenge if it does not.  </p>
  
  <p>Is a cookie the only way to attach data to an HTTP conversation, and persist it on the client side during navigation?</p>
  
  <p>Are JWTs purely for SinglePageApps (SPAs) where a page is initially served, and then all subsequent data is handled by Ajax (where I can set the header no problem)?</p>
  
  <p>Alternatively, is it possible to write the webpages such that they always populate the Authorization header with the access token, if it exists?</p>
  
  <p>I realise that I may have thought myself into a knot, any help would be appreciated.</p>