Require authorization on ALL Blazor pages
You can do this by adding a authorization fallback policy :
services.AddRazorPages();
services.AddAuthorization(options =>
{
options.FallbackPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
});
The fallback authentication policy requires all users to be authenticated, except for Razor Pages, controllers, or action methods with an authentication attribute.
That means that you can use the attributes for Example @attribute [Authorize]
(attributes) to customize Authentication and Authorization.
I believe that will work... Place the following code snippet in the _Imports.razor file
@using Microsoft.AspNetCore.Authorization
@attribute [Authorize]
In that case, when the Index page is hit, the user will be redirected to the Login page. If you want to perform authentication before the Blazor App is being render, add the code snippet from above in the _Host.cshtml file
Add the @attribute [AllowAnonymous]
to specific pages you want to exculde from authentication, as for instance, the Index page.