There are three possibilities for getting the user in a component (a page is a component):
- Inject
IHttpContextAccessor
and from it accessHttpContext
and thenUser
; need to registerIHttpContextAccessor
inStartup.ConfigureServices
, normally usingAddHttpContextAccessor
. Edit: according to the Microsoft docs you must not do this for security reasons. - Inject an
AuthenticationStateProvider
property, callGetAuthenticationStateAsync
and get aUser
from 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.