title: Blazor @attribute [Authorize] tag is not working
body: <p>I have a working .NET Core 3.0 MVC website, using AzureAD for authentication, this all works fine. I have started to migrate some of the front-end pages to Blazor (in same project) but cannot get authentication to work.</p>
  
  <p>I have added the @attribute [Authorize] tag to the top of Index.razor but I do not get redirected to Azure to login as I would do when adding it to a standard ASP.NET MVC Controller.</p>
  
  <p>Startup.ConfigureServices</p>
  
  <pre><code>services.AddAuthentication(options =&gt;
  {
      options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
      options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
      options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
  })
  .AddCookie()
  .AddOpenIdConnect(options =&gt;
  {
      Configuration.GetSection("OpenIdConnect").Bind(options);
  });
  services.AddAuthorizationCore(options =&gt;
  {
      options.AddPolicy(Policies.AccessRole, Policies.IsAccessPolicy());
      options.AddPolicy(Policies.AdminRole, Policies.IsAdminPolicy());
  });
  </code></pre>
  
  <p>Startup.Configure</p>
  
  <pre><code>app.UseAuthentication();
  app.UseAuthorization();
  </code></pre>
  
  <p>Index.razor</p>
  
  <pre><code>@page "/"
  @attribute [Authorize(Policy = Policies.AccessRole)]
  </code></pre>
  
  <p>Policies</p>
  
  <pre><code>public static class Policies
  {
      public const string AccessRole = "Access";
      public const string AdminRole = "Admin";
  
      public static AuthorizationPolicy IsAccessPolicy()
      {
          return new AuthorizationPolicyBuilder().RequireAuthenticatedUser()
                                                 .RequireRole(AccessRole)
                                                 .Build();
      }
  
      public static AuthorizationPolicy IsAdminPolicy()
      {
          return new AuthorizationPolicyBuilder().RequireAuthenticatedUser()
                                                 .RequireRole(AdminRole)
                                                 .Build();
      }
  }
  </code></pre>
  
  <p>If I navigate to an MVC page I get authenticated by AzureAD, if I then return to the Blazor page I can use the following successfuly</p>
  
  <pre><code>&lt;AuthorizeView Policy="@Policies.AccessRole"&gt;
      &lt;p&gt;Is in Access policy.&lt;/p&gt;
  &lt;/AuthorizeView&gt;
  
  &lt;AuthorizeView Policy="@Policies.AdminRole"&gt;
      &lt;p&gt;Is in Admin policy.&lt;/p&gt;
  &lt;/AuthorizeView&gt;
  </code></pre>
  
  <p>So to summarise, my Blazor page is not automatically issuing the auth challenge when using the [Authorize] attribute.</p>
  
  <p>Does anyone know what I am doing wrong?</p>
  
  <p><strong>Update</strong></p>
  
  <p>It's as designed
  <a href="https://github.com/aspnet/AspNetCore/issues/13709" rel="noreferrer">https://github.com/aspnet/AspNetCore/issues/13709</a></p>
  
  <p>As a workaround I have added a component to redirect to a login page</p>
  
  <p>App.razor</p>
  
  <pre><code>&lt;Router AppAssembly="@typeof(Program).Assembly"&gt;
      &lt;Found Context="routeData"&gt;
          &lt;AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"&gt;
              &lt;NotAuthorized&gt;
                  &lt;AuthChallenge&gt;&lt;/AuthChallenge&gt;
              &lt;/NotAuthorized&gt;
          &lt;/AuthorizeRouteView&gt;
      &lt;/Found&gt;
      &lt;NotFound&gt;
          &lt;CascadingAuthenticationState&gt;
              &lt;LayoutView Layout="@typeof(MainLayout)"&gt;
                  &lt;p&gt;Sorry, there's nothing at this address.&lt;/p&gt;
              &lt;/LayoutView&gt;
          &lt;/CascadingAuthenticationState&gt;
      &lt;/NotFound&gt;
  &lt;/Router&gt;
  </code></pre>
  
  <p>AuthCallenge.razor</p>
  
  <pre><code>@inject NavigationManager Navigation
  
  @code {
      protected override void OnInitialized()
      {
          Navigation.NavigateTo("/Account/SignIn", true);
      }
  }
  </code></pre>