title: Blazor WASM Authentication Static Web App
body: <p>I've got a problem which I quite don't understand yet (used doc: <a href="https://docs.microsoft.com/en-us/aspnet/core/blazor/security/?view=aspnetcore-6.0" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/aspnet/core/blazor/security/?view=aspnetcore-6.0</a>) , if I try to <strong>login</strong>, it <strong>works</strong> on the Static Web App (<strong>SWA</strong>) and <strong>locally</strong>.
  <a href="https://i.stack.imgur.com/z7QB4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/z7QB4.png" alt="locally" /></a>
  <a href="https://i.stack.imgur.com/koYSv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/koYSv.png" alt="SWA" /></a></p>
  <p>but when I try to <strong>logout</strong>,
  <strong>locally</strong> it <strong>works</strong> and on the <strong>SWA</strong> it <strong>doesn't</strong>.</p>
  <p><a href="https://i.stack.imgur.com/Dizn6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Dizn6.png" alt="locally" /></a>
  <a href="https://i.stack.imgur.com/zXkVJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zXkVJ.png" alt="SWA" /></a></p>
  <p>Anyone has an idea what the problem is?
  Screenshots for reference:</p>
  <p>Portal app registration:
  <a href="https://i.stack.imgur.com/NhWWq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NhWWq.png" alt="POrtal" /></a></p>
  <p>appsettings.json</p>
  <pre><code>{
    &quot;AzureAd&quot;: {
      &quot;Authority&quot;: &quot;https://login.microsoftonline.com/TENANT-ID&quot;,
      &quot;ClientId&quot;: &quot;CLIENT-ID&quot;,
      &quot;ValidateAuthority&quot;: true
    }
  }
  </code></pre>
  <p>staticwebapp.config.json</p>
  <pre><code>{
    &quot;networking&quot;: {
      &quot;allowedIpRanges&quot;: [
        &quot;IP/MASK&quot;
      ]
    }
  }
  </code></pre>
  <p>Program.cs</p>
  <pre><code>public class Program
      {
          public static async Task Main(string[] args)
          {
              var builder = WebAssemblyHostBuilder.CreateDefault(args);
              builder.RootComponents.Add&lt;App&gt;(&quot;#app&quot;);
  
              builder.Services.AddSingleton&lt;IProductService&lt;ProductA,InsuredPerson&gt;, ProductAService&gt;();
              builder.Services.AddSingleton&lt;IProductService&lt;ProductB,InsuredPerson&gt;, ProductBService&gt;();
              builder.Services.AddSingleton&lt;IDownloadService,DownloadService&gt;();
              builder.Services.AddSingleton&lt;IUploadService, UploadService&gt;();
              builder.Services.AddAutoMapper(new[] { typeof(ServiceMappingProfile)});
              builder.Services.AddHttpClient();
      
              builder.Services.AddScoped(sp =&gt; new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
              builder.Services.AddLocalization();
  
  
              builder.Services.AddMsalAuthentication(options =&gt;
              {
                  builder.Configuration.Bind(&quot;AzureAd&quot;, options.ProviderOptions.Authentication);
                  options.ProviderOptions.DefaultAccessTokenScopes
                      .Add(&quot;https://graph.microsoft.com/User.Read&quot;);
                  options.ProviderOptions.LoginMode = &quot;redirect&quot;;
              });
              
  
              var host = builder.Build();
  
              CultureInfo culture;
  
              var js = host.Services.GetRequiredService&lt;IJSRuntime&gt;();
  
              var result = await js.InvokeAsync&lt;string&gt;(&quot;blazorCulture.get&quot;);
  
              if (result != null)
              {
                  culture = new CultureInfo(result);
              }
              else
              {
                  culture = new CultureInfo(&quot;en-US&quot;);
                  await js.InvokeVoidAsync(&quot;blazorCulture.set&quot;, &quot;en-US&quot;);
              }
              CultureInfo.DefaultThreadCurrentCulture = culture;
              CultureInfo.DefaultThreadCurrentUICulture = culture;
  
              await host.RunAsync();
          }
      }
  </code></pre>
  <p>App.razor</p>
  <pre><code>&lt;CascadingAuthenticationState&gt;
      &lt;Router AppAssembly=&quot;@typeof(App).Assembly&quot;&gt;
          &lt;Found Context=&quot;routeData&quot;&gt;
              &lt;AuthorizeRouteView RouteData=&quot;@routeData&quot; DefaultLayout=&quot;@typeof(MainLayout)&quot;&gt;
                  &lt;NotAuthorized&gt;
                      @if (context.User.Identity?.IsAuthenticated != true)
                      {
                          &lt;RedirectToLogin /&gt;
                      }
                      else
                      {
                          &lt;p role=&quot;alert&quot;&gt;You are not authorized to access this resource.&lt;/p&gt;
                      }
                  &lt;/NotAuthorized&gt;
              &lt;/AuthorizeRouteView&gt;
              &lt;FocusOnNavigate RouteData=&quot;@routeData&quot; Selector=&quot;h1&quot; /&gt;
          &lt;/Found&gt;
          &lt;NotFound&gt;
              &lt;PageTitle&gt;Not found&lt;/PageTitle&gt;
              &lt;LayoutView Layout=&quot;@typeof(MainLayout)&quot;&gt;
                  &lt;p role=&quot;alert&quot;&gt;Sorry, there's nothing at this address.&lt;/p&gt;
              &lt;/LayoutView&gt;
          &lt;/NotFound&gt;
      &lt;/Router&gt;
  &lt;/CascadingAuthenticationState&gt;
  </code></pre>