title: How to require authentication in ASP.NET Core 5 Blazor Server Mode with redirect to login
body: <p>How do I configure BlazorHub endpoint to require authenticated user with automatic redirect to login if not authenticated?</p>
  <p>I have tried to configure default authorization policy as well as call <code>RequireAuthenticated</code> on BlazorHub endpoint builder, but I'm still not redirected to login page when I run the Blazor app.</p>
  <pre><code>public class Startup
  {
      public void ConfigureServices(IServiceCollection services)
      {
          services.AddRazorPages();
          services.AddServerSideBlazor();
  
          services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
              .AddCookie(); // added by me
          services.AddAuthorization(options =&gt;
          {
              options.DefaultPolicy = new AuthorizationPolicyBuilder()
                  .RequireAuthenticatedUser()
                  .Build();
          }); // added by me
      }
  
      // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
      public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
      {
          if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
          }
          else
          {
              app.UseExceptionHandler(&quot;/Error&quot;);
              // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
              app.UseHsts();
          }
  
          app.UseHttpsRedirection();
          app.UseStaticFiles();
  
          app.UseRouting();
  
          app.UseAuthentication(); // added by me
          app.UseAuthorization();  // added by me
  
          app.UseEndpoints(endpoints =&gt;
          {
              endpoints.MapControllers();
              endpoints.MapBlazorHub()
                  .RequireAuthorization(); // added by me
              endpoints.MapFallbackToPage(&quot;/_Host&quot;);
          });
      }
  }
  </code></pre>