title: How can I access the browsers localStorage in Blazor?
body: <p>I want to support JWTs, so I need to keep the token around; is there some facility to access this? Or should we just be registering our own javascript function to access this functionality for now?</p>
  
  <p>Edit: per advice, I attempted to use JS interop as :</p>
  
  <pre><code>&lt;script&gt;
      localStorage.setItem("key1", "key1data");
      Blazor.registerFunction("readStorage", (key) =&gt; {
          return localStorage.getItem(key);
      });
  &lt;/script&gt;
  @if (jwtKey == null)
  {
  &lt;div class="login-panel"&gt;
      &lt;p&gt;JWT not loaded&lt;/p&gt;
  &lt;/div&gt;
  }
  else
  {
  &lt;div class="login-panel"&gt;
      &lt;p&gt;@jwtKey&lt;/p&gt;
  &lt;/div&gt;
  }
  
  @functions {
      public RenderFragment Body { get; set; }
      string jwtKey;
  
      protected override async Task OnInitAsync()
      {
          jwtKey = RegisteredFunction.Invoke&lt;string&gt;("readStorage", "key1");
          if (jwtKey == null)
          {
              jwtKey = "Unknown";
          }
      }
  }
  </code></pre>
  
  <p>But this results in a WASM error in diag:</p>
  
  <blockquote>
    <p>WASM:
    [Microsoft.AspNetCore.Blazor.Browser.Interop.JavaScriptException]
    Could not find registered function with name 'readStorage'. WASM:
    Error: Could not find registered function with name 'readStorage'.</p>
  </blockquote>
  
  <p>FYI, this is in the MainLayout.cshtml of the Blazor VS boilerplate project.</p>
  
  <p>(can make a new Question if appropriate; somewhat related to this one though)</p>