'Resource-based authorization / imperative authorization for Blazor Server

After studying the documentation, I understand how role-based, policy-based and claim-based authorization are managed by ASP.NET Core. I also understand how resource-based authorization works, however since the example given in the documentation is based on the MVC framework I am trying to understand where the authorization service is supposed to be injected in blazor server components.

It seems to me that injecting the service on every page would be quite tedious, so I am wondering if Blazor Server has planned a better extensibility point so that the semantics of resource-based authorization are more akin to overriding a virtual method (or a similar approach that would allow code reuse through inheritance or composition or anything else). Hopefully my question is clear enough.



Solution 1:[1]

Your question is legitimate because authentication is quite complex.

This example is exactly what you are looking for. https://github.com/iso8859/AspNetCoreAuthMultiLang

The idea is to have a base class (Pages/LoadUserContext.cs) that handles authentication object AuthenticationStateProvider.

Each Blazor page inherits from this base class @inherits LoadUserContext

With this technic you have _masp object available in each page, this object contains all you need about user.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Remi THOMAS