Is Razor's AuthorizeView-component dependent on the existence of a default authentication scheme?

Yakup Ipek 0 Reputation points
2023-08-10T10:08:36.3166667+00:00

When a default authentication scheme is not provided during registration of the authentication service the user does not get authenticated, hence the items contained within the authorize-view-component do not get rendered.

Hence my question: Does authorization via the AuthorizeView-component only work when a default authentication scheme is provided?

My attempts to solve it differently, like calling RequireAuthorization on endpoints.MapBlazorHub() did break the logic of the AuthorizeView-component as the NotAuthorized component does not work in this case.

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,420 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,312 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 57,886 Reputation points
    2023-08-10T17:30:32.74+00:00

    the AuthorizeView component does not require authentication, it only display content based on the current state.

    a Blazor app is a single http request. this request is either authorized or not, so the authorization state is the same for the life of the app. its either authorized or not. if you require authorization at startup it will always be authorized.

    a typical scenario is the blazor app starts as not authorized. the AuthorizeView comports will show only public content. there should be a login option in the unauthorized content. when clicked, it redirects to the login page (unloading the blazor app). after a successful login, the login page redirects back to the Blazor app page. this reloads the Blazor app with an authenicated request. Now the AuthorizeView will display authorized content.

    note: Blazor has added support to update the authentication state while running. so if using custom authentication, not oauth (which always requires a redirect), you should be able to login/logout without the unload. there is no built in support for this scenario (but AuthorizeView supports), and while a little complex is doable. Besides building Blazor components to login/login out you will need a custom AuthenticationStateProvider that the component call to update the authentication status.