There are three possibilities for getting the user in a component (a page is a component):
- Inject
IHttpContextAccessorand from it accessHttpContextand thenUser; need to registerIHttpContextAccessorinStartup.ConfigureServices, normally usingAddHttpContextAccessor. Edit: according to the Microsoft docs you must not do this for security reasons. - Inject an
AuthenticationStateProviderproperty, callGetAuthenticationStateAsyncand get aUserfrom it - Wrap your component in a
<CascadingAuthenticationState>component, declare aTask<AuthenticationState>property and call it to get theUser(similar to #2)
See more here: https://learn.microsoft.com/en-us/aspnet/core/security/blazor.