title: Client side Blazor authentication token expired on server side
body: <p>I'm having a problem with client side token with blazor authentication. I implemented the authentication based on this blog posts, I'm using webassembly project.</p>
  
  <p><a href="https://chrissainty.com/securing-your-blazor-apps-introduction-to-authentication-with-blazor/" rel="noreferrer">https://chrissainty.com/securing-your-blazor-apps-introduction-to-authentication-with-blazor/</a></p>
  
  <p><a href="https://chrissainty.com/securing-your-blazor-apps-authentication-with-clientside-blazor-using-webapi-aspnet-core-identity/" rel="noreferrer">https://chrissainty.com/securing-your-blazor-apps-authentication-with-clientside-blazor-using-webapi-aspnet-core-identity/</a></p>
  
  <p><a href="https://chrissainty.com/securing-your-blazor-apps-configuring-role-based-authorization-with-client-side-blazor/" rel="noreferrer">https://chrissainty.com/securing-your-blazor-apps-configuring-role-based-authorization-with-client-side-blazor/</a></p>
  
  <p>Almost everything works fine, but I'm having an issue. On server side the authentication token gets expired but on client side, I still have the authentication token on local storage. My function to get local state:</p>
  
  <pre><code>   public override async Task&lt;AuthenticationState&gt; GetAuthenticationStateAsync()
      {
          var savedToken = await _localStorage.GetItemAsync&lt;string&gt;("authToken");            
  
          if (string.IsNullOrWhiteSpace(savedToken))
          {
              return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
          }
  
          _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", savedToken);
  
          return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(ParseClaimsFromJwt(savedToken), "jwt")));
      }
  </code></pre>
  
  <p>Well, looking on response header I can see that server tells me that my local token is expired, but I don't know how to get this information on client side. So my client side tells me that I'm authenticated but on server side I'm not. I don't want to make a request to test this every time that my method GetAuthenticationStateAsync runs to clean manually the token of local storage. How is the best way to deal with this behavor? I'm missing someting on my code?</p>
  
  <p>The header response: "www-authenticate: Bearer error="invalid_token", error_description="The token expired at '02/24/2020 11:52:35'""</p>
  
  <p>Thanks.</p>