Edit: I actually found a problem with the first solution where it would not allow me to have any endpoint or page that did not require authorization. I did find this link and it works like a charm.
I was looking for a solution for this myself and found the following link. So far it seems to work as expected.
New solution:
//RedirectToLogin
@inject NavigationManager NavigationManager
@code{
protected override async Task OnInitializedAsync()
{
var returnUrl = "~/" + NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
NavigationManager.NavigateTo($"Identity/Account/Login?returnUrl={returnUrl}", forceLoad:true);
}
//App.razor
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
<RedirectToLogin />
</NotAuthorized>
<Authorizing>
<p>Authorizing...</p>
</Authorizing>
</AuthorizeRouteView>
//this in the page I want authorization for
@attribute [Authorize]
Old solution:
I put the following in my ConfigureServices:
// Add a default AuthorizeFilter to all endpoints
services.AddRazorPages()
.AddMvcOptions(options => options.Filters.Add(new AuthorizeFilter()));