Yes, these three objects are commonly used in Blazor server-side applications for handling user authentication and authorization:
AuthenticationState: Represents the authentication state of the user, including the user's claims.
IdentityUser: Represents the ASP.NET Identity user, which contains user information like email, phone, etc.
User (App User): Represents your application-specific user object, which is associated with the IdentityUser through a relationship like User.AspUserId == IdentityUser.Id.
You can use the AuthenticationStateChanged event to track changes in the user's authentication state and update your IdentityUser and User objects accordingly. This way, you don't have to re-read them for every page. To store these objects per session, you can use a scoped service:
Create a scoped service that holds the IdentityUser and User objects.
Register the service in the Startup.cs or Program.cs file, depending on your Blazor version.
Inject the service into your components and access the stored IdentityUser and User objects.
This approach ensures that the objects are stored and shared per session.
Your GetUser() method is a valid way to retrieve the IdentityUser object. However, you can improve it by including null checks and exception handling to make it more robust. Additionally, you can consider moving this method to a separate service (e.g., UserService) to keep your code modular and maintainable.
You seem to be on the right track with handling user authentication and authorization in your Blazor server-side application. Some additional tips to consider:
Make sure to protect your components and routes using the [Authorize] attribute or a custom authorization policy.
Leverage Blazor's built-in components, like AuthorizeView, to conditionally display content based on the user's authentication or authorization status.
Use the UserManager and SignInManager classes from ASP.NET Core Identity to handle user management tasks, such as creating users, validating passwords, and signing in.
To improve security and user experience, consider implementing features like two-factor authentication, account lockout, and password reset.